Monday, 6 July 2009

Bug in Selenium RC, affecting XPath locators in Internet Explorer

I've recently noticed a bug in Selenium RC, affecting XPath locators in Internet Explorer. The issue is actually a integration bug between ill-formatted HTML and Selenium. If the HTML element you are trying to 'locate', has a class attribute beginning with a space - then selenium won't be able to locate the element using that class in the XPath. e.g.:
Your HTML:

<div class="outer blue">
<div class=" inner green">
</div>
</div>

The Selenium RC command, is shown below.
Firefox seems to correctly handle the leading space:

# Will return 1 in FireFox, and 0 in Internet Explorer
browser.get_xpath_count("//*[@class='inner green']")

...while IE doesn't.

# Will return 1 in FireFox, and 1 in Internet Explorer
browser.get_xpath_count("//*[@class='outer blue']")

You may argue (as I have) that the class attribute shouldn't have a leading space. In the Spec, spaces are described as being separators between class attribute values. So the creators/maintainers of the page should 'fix' the HTML, allowing the selenium code/tests to correctly locate the element.

1 comments:

Mattias said...

I've stopped using Seleniums built-in methods for getting information, it's too slow. I'm using Hpricot instead.