Question

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']/../"
Was it helpful?

Solution

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

OTHER TIPS

For current versions of Watir the better way to do this would be

browser.img(:src => '3.jpg').parent.text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top