Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve text from span element in watir webdriver

I have the following:

<ul class="a">
  <li class="b">
    <a class="c">
      <span class="d">
        <strong>text</strong>
      </span>
    </a>
  </li>
</ul>

I'm trying to get the "text" from the span.

When I try:

puts browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d')

I get the span element back. #<Watir::Span:0x007fa1d11edb18> as i should.

But when trying:

puts browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d').text

im getting an error saying :

unable to locate element, using {:class=>'b', :tag_name=>"li"}

Any ideas ?

like image 563
MichaelR Avatar asked Dec 20 '22 08:12

MichaelR


1 Answers

It would help if you could point to the page where the problem is reproducible, even if that means that you create a page yourself, put it in dropbox and make it public.

Did you even try to replicate the problem with the html and ruby code that you have posted? I am asking because the problem is not reproducible with the provided code. Take a look yourself:

> browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d')
 => #<Watir::Span:0x..fa66c75ff618434cc located=false selector={:class=>"d", :tag_name=>"span"}> 

> browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d').text
 => "text"

As far as I understood, you are surprised that when you provide some code, Watir returns an element, but when you want to execute a method on the element, it complains that it can not find the element.

Take a closer look at the ruby object that represents element on a page:

#<Watir::Span:0x..fa66c75ff618434cc located=false selector={:class=>"d", :tag_name=>"span"}>

Please notice located=false. As far as I know, that means that watir creates a ruby object before trying to access it, so the first line of your code does not raise any exceptions. But then when you try to use the element, watir can not find it on the page and complains.

like image 158
Željko Filipin Avatar answered Feb 15 '23 20:02

Željko Filipin