wim
wim

Reputation: 362657

Syntax for using regex in ruby / watir webdriver

I am able to find an element I'm looking for using an exact match on the text, as shown below, but when I try to extend this to using a regex it doesn't work. What am I doing wrong?

>> 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"}>

EDIT based on comments, found following HTML that seems to be what he's after, on the afl site. This is the only match in the page html for a case insensitive search for "latest news'

<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">
      .........                             

Upvotes: 3

Views: 4190

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

I cannot reproduce your results given the test site. For me, all of the locators using 'Latest News' return false (ie both string and regex matches). The page text is in all caps, 'LATEST NEWS'. Using that instead of 'Latest News' allowed both the string and regex locators to return 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

Upvotes: 4

Related Questions