tags:

views:

589

answers:

1

I'm using the Ruby Watir library to do automated testing for a client and I'm having issues with the XPath selector. I think I just need another set of eyes to let me know if I'm just missing something.

Here is the selector I'm using:

puts ie.cell(:xpath, "//img[@src='3.jpg']/../").text

For this set of tables, it works as expected and prints "Third Image":

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="2.jpg">Second Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

But is is breaking when I remove the second table:

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

Using the puts code above I get this error on the second example:

Watir::Exception::UnknownObjectException: Unable to locate element, using :xpath, "//img[@src='3.jpg']/../"

+1  A: 

I reproduced the problem, and restarting the browser (IE6) fixed it for me.

Željko Filipin
Starting a new IE instance did fix the issue. There are a lot of weird bugs I experienced with this though. I had an html file on my desktop that I was testing against. If I modified the html file and refreshed the page, the xpath selector wouldn't work consistently at all. However, using other selectors, such ad :id would work fine. Another example of something that wouldn't work is if I changed the src attributes, the xpath selector would not pick up the changes after a page refresh.
Andrew Hampton
I am not sure what is going on. Sounds like a caching problem. If you want a better answer, post this to watir-general group, there are more people that know their way around Watir there.
Željko Filipin