4
votes

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 ?

1
Based on the html structure, I would guess that it is a dropdown menu. If so, you need to expand the menu before you can click any of its links (ie Watir can only interact with elements that are visible to the user).Justin Ko

1 Answers

11
votes

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.