[英]Click on particular instance for a button using CSS selector
I have a webpage which has multiple submit buttons.我有一个有多个提交按钮的网页。 I want to loop through them and click on each of them one by one.
我想遍历它们并一一单击它们。
I know I can do it via xpath like this (//button[@class='submit'])[i]
(where i = loop number).我知道我可以通过 xpath 像这样
(//button[@class='submit'])[i]
(其中 i = 循环编号)。
But I wanted to know if it's possible to do via CSS selector?但我想知道是否可以通过 CSS 选择器进行操作?
I've tried button.submit
but it will always click on the first button and I want to be able to loop through.我试过
button.submit
但它总是会点击第一个按钮,我希望能够循环播放。 I've also tried button.submit:first-child
but it seems to do the same thing.我也试过
button.submit:first-child
但它似乎做同样的事情。
The following is similar to what the HTML is like.下面是类似HTML的样子。
<div>
<button class="submit" type="button"></button>
</div>
<div>
<button class="submit" type="button"></button>
</div>
<div>
<button class="submit" type="button"></button>
</div>
Yes, you can do this such way:是的,您可以这样做:
If you are using Java version less than 8 do this way:如果您使用的是小于 8 的 Java 版本,请执行以下操作:
List<WebElement> elements = driver.findElements(By.cssSelector("button.submit"));
WebElement confirm = driver.findElement(By.cssSelector("selector_for_confirm"));
for(WebElement element: elements){
element.click();
confirm.click();
}
If you are using Java 8 or above, you can try this way:如果您使用的是 Java 8 或更高版本,您可以这样尝试:
List<WebElement> elements = driver.findElements(By.cssSelector("button.submit"));
WebElement confirm = driver.findElement(By.cssSelector("selector_for_confirm"));
elements.forEach(e->{
e.click();
confirm.click();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.