A :nth-match pseudo-selector has been proposed for CSS Selectors Level 4:
http://dev.w3.org/csswg/selectors4/#nth-match
There are no browser implementations yet AFAIK.
While WebDriver implementations do inject Sizzle in certain cases,
they prefer native CSS implementations so Sizzle specific features
like :eq should be avoided.
I suppose the official WebDriver way to do this with CSS would be
something like this (untested) code sample:
WebElement nthMatch = null;
Integer n = 5; // CSS is 1-indexed
String cssSelector = "div.something > a";
List<WebElement> matches =
driver.findElements(By.ByCssSelector(cssSelector));
if (n <= matches.size() {
nthMatch = matches.get(n - 1); // Java Lists are 0-indexed
}
You could make this nicer to use by wrapping it up in a
ByCssSelectorNthMatch class that subclasses By:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/By.html
--
Benjamin Hawkes-Lewis