Usha Guduri

Finding Watir Elements with XPATH

We recently got into automated ui testing with watir.

First impression: Awesome!

Then we added rspec and rake when the nightmares began - more about that later perhaps.

For a simple page with a few DOM elements, its pretty straightforward. Enter a highly dynamic, complex rendering logic and the DOM access breaks down. Something even as intellectually simple as:


actually coughed and puffed with the watir-webdriver <0.5 versions, specifically on Firefox. But of course, there is a workaround - XPATH! We are ‘Engineers’ afterall!

browser.element(:xpath => //div[id=myid]/div[class=myclass])

The problem with XPath though, is that its absolute and highly specific. If you so much as change the class from

<div class=”myclass myclass2>

Xpath breaks and so do your tests.

Solution: Update to watir-webdriver 0.5.2

Lesson: Be wary of anything < 1.0