Reputation: 951
SeleniumHQ says that each driver supports whatever CSS Selectors its browser supports. According to this site, IE 9 should support the :nth-of-type() selector. However, I appear to be getting a NullPointerException
from the depths of the RemoteWebDriver
class when I execute findElements
on this selector. My By.cssSelector
looks like this:
table#ucsp_dgMultiSelect tr:nth-of-type(2) input#cbPres
This works fine on Chrome. Maybe IE 9 has a problem with putting that selector in the middle there, I don't know, but that would break a lot of my code. The :nth-of-type() selector has become my go-to for identifying WebElement locators within a table. Has anyone else had success using the :nth-of-type() selector as a locator with WebDriver and IE 9?
I'm using IEDriverServer.exe.2.25.2.0 and my IE version is 9.0.8112.16421 64-bit
Upvotes: 0
Views: 1981
Reputation: 27486
WebDriver does, in fact, support the CSS selectors that the browser does, and if IE9 supports the :nth-of-type()
selector, then so should the IE driver. However, that's not the entirety of the story. If the DOCTYPE
in your page is not a modern standard (e.g. <!DOCTYPE html>
, IE also tries to guess at how it's supposed to render the document, and if it guesses that it should be rendering it as a previous IE version, it'll use the CSS selector engine of that previous version.
Since you're using IE9, there's a very easy way to see if WebDriver should be able to find the element with the selector you're trying to use. Open the "F12 Developer Tools" by hitting the function key F12 while you're on the page you're interested in. Go to the Script tab in the Developer tools, and type
document.querySelector('table#ucsp_dgMultiSelect tr:nth-of-type(2) input#cbPres')
If the console displays the element information, then WebDriver should be able to find it. If not, then IE can't find it, and WebDriver never will either.
This debugging technique will work anytime you're trying to find an element using CSS selectors in IE9, and is invaluable in helping find out if the problem is IE or if it's the driver. Additionally, the F12 Developer Tools also will tell you what mode IE is trying to render the page in, which can also be instructive.
Upvotes: 2