简体   繁体   中英

Is there a selenium cssSelector equivalent to get the “nth” matching element?

Here is my sample jQuery which returns 10 matching elements on a page:

$('input.text-form-field');

Since I only need the 3rd element, I used the below and it works fine:

$('input.text-form-field').get(2);

Can someone suggest a proper selenium cssSelector equivalent to achieve the same result?

Simply using

By.cssSelector("input.text-form-field")

will fail as it matches against 10 elements on the page !

Any help in this regard would be greatly appreciated.

我认为无法使用CSS选择器来完成此操作,但是您可以使用XPath来完成。

By.XPath("(//input[@class=\"text-form-field\"])[2]")

You can do something like this

List<WebElement> elements = driver.findElements(By.className("text-form-field"));
for (WebElement element : elements) 
{
element.click(); // iterate or do get(2) for third element
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM