簡體   English   中英

在ruby / watir webdriver中使用正則表達式的語法

[英]Syntax for using regex in ruby / watir webdriver

我能夠在文本中使用完全匹配找到我正在尋找的元素,如下所示,但是當我嘗試將其擴展為使用正則表達式時,它不起作用。 我究竟做錯了什么?

>> browser.h3(:text => 'Latest News').exist?
=> true
>> browser.h3(:text, 'Latest News').exist?
=> true
>> browser.h3(:text, /Latest News/).exist?
=> false

>> p browser.h3(:text, 'Latest News')
#<Watir::Heading:0x..fcb7790992119c2b2 located=false selector={:text=>"Latest News", :tag_name=>"h3"}>
=> #<Watir::Heading:0x..fcb7790992119c2b2 located=false selector={:text=>"Latest News", :tag_name=>"h3"}>
>> p browser.h3(:text, /Latest News/)
#<Watir::Heading:0x54fac9ec99992782 located=false selector={:text=>/Latest News/, :tag_name=>"h3"}>
=> #<Watir::Heading:0x54fac9ec99992782 located=false selector={:text=>/Latest News/, :tag_name=>"h3"}>

根據評論進行編輯 ,在afl網站上發現以下似乎是他所追求的HTML。 這是頁面html中唯一一個不區分大小寫搜索“最新新聞”的匹配項

<div class="double-col column">
  <div class="segment   ">
    <h3 class="section-header    blocked">
        LATEST NEWS</h3>
    <div class="content">
      <div class="list-item focus">
        <div class="img-holder">
      .........                             

鑒於測試網站,我無法重現您的結果。 對我來說,使用“最新消息”的所有定位器都返回false(即字符串和正則表達式匹配)。 頁面文字全部大寫,“最新消息”。 使用它而不是“最新消息”允許字符串和正則表達式定位器返回true。

require 'watir'

browser = Watir::Browser.new :chrome
browser.goto 'afl.com.au'

puts browser.h3(:text => 'Latest News').exist?
#=> false
puts browser.h3(:text, 'Latest News').exist?
#=> false
puts browser.h3(:text, /Latest News/).exist?
#=> false

puts browser.h3(:text => 'LATEST NEWS').exist?
#=> true
puts browser.h3(:text, 'LATEST NEWS').exist?
#=> true
puts browser.h3(:text, /LATEST NEWS/).exist?
#=> true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM