Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for using regex in ruby / watir webdriver

Tags:

regex

ruby

watir

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">
      .........                             
like image 313
wim Avatar asked Aug 14 '26 21:08

wim


1 Answers

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
like image 166
Justin Ko Avatar answered Aug 17 '26 12:08

Justin Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!