Selenium Java – 如何在网页元素路径中存储变量,以便循环遍历多个元素

huangapple go评论105阅读模式
英文:

Selenium Java - how to store a variable in web element path in order to loop through multiple elements

问题

我一直在尝试使用Selenium Java遍历多个网页元素:

for (int i = 0; i < 100; i++) {

driver.findElements(By.cssSelector("#matchListWithMessages > div.messageList > a:nth-child(5)")).click();

}

然而,这些网页元素具有不同的nth-child值,因此我需要使css选择器路径像一个变量一样,例如,我尝试用循环的变量i来改变nth child的数字,这样它就会逐个遍历所有网页元素,就像这样:

for (int i = 0; i < 100; i++) {

driver.findElements(By.cssSelector("#matchListWithMessages > div.messageList > a:nth-child(i)")).click();

}

但是由于括号内的代码是字符串,它将i视为字符串而不是变量。是否有任何方法可以使它正确,使我的循环将i视为变量并逐个遍历所有网页元素?

英文:

I have been trying to loop through multiple web elements with Selenium java:

for (int i = 0; i &lt; 100; i++) {

driver.findElements(By.cssSelector(&quot;#matchListWithMessages &gt; div.messageList &gt; a:nth-child(5)&quot;)).click();

}

However these web elements have different nth-child values, therefore I need to make a cssselector path like a variable, for example, i'm trying change a nth child number with a variable i of my loop, so it would go through all web elements one by one, like this:

for (int i = 0; i &lt; 100; i++) {

driver.findElements(By.cssSelector(&quot;#matchListWithMessages &gt; div.messageList &gt; a:nth-child(i)&quot;)).click();

}

But since the code inside the brackets are String, it reads i like a String and not like a variable. Is there any way to make it right, that my loop would read i as a variable and go through all web elements one by one?

答案1

得分: 1

Here's the translation:

或者您可以使用以下语法:

for (int i = 0; i < 100; i++) {
    String selector = String.format("#matchListWithMessages > div.messageList > a:nth-child(%d)", i);
    driver.findElement(By.cssSelector(selector)).click();
}
英文:

Or you can use the following syntax:

for (int i = 0; i &lt; 100; i++) {
    String selector = String.format(&quot;#matchListWithMessages &gt; div.messageList &gt; a:nth-child(%d)&quot;, i);
    driver.findElement(By.cssSelector(selector)).click();
}

答案2

得分: 0

To strictly solve your problem, you can use the following piece of code:

for (int i = 0; i < 100; i++) {
    String csspath = "#matchListWithMessages > div.messageList > a:nth-child(" + i + ")";
    driver.findElement(By.cssSelector(csspath)).click();
}

Also, please note that in your code, you have used driver.findElements instead of driver.findElement. The former returns a list of web elements, and the click method won't work on a list type. However, the latter returns a web element type object, and you will be able to click it. For more information, refer to: https://www.browserstack.com/guide/findelement-in-selenium

The issue with your technique is that if there are fewer than 100 elements, your code will fail, or it will only click 100 if there are more. A more suitable solution would be to find a common locator available for your elements. Then you can use driver.findElements(By.cssSelector(locator)) to get a list of all such elements and loop through the list to click all of them. This way, you won't need to worry about how many such elements are on the page.

英文:

To strictly solve your problem. You can use below piece of code.

for (int i = 0; i &lt; 100; i++) {
String csspath = &quot;#matchListWithMessages &gt; div.messageList &gt; a:nth-child(&quot;+i+&quot;)&quot;;
driver.findElement(By.cssSelector(csspath )).click();

}

Also please note in your code you have used driver.findElements instead of driver.findElement. As former returns a list of web-element and click method won't work on list type, however later will return a web-element type object and you will be able to click it. See below for more information on same:
[https://www.browserstack.com/guide/findelement-in-selenium][1]

Also, Issues with your technique is that if there are less than 100 elements your code will fail or it will click only 100 if there are more. A more suitable solution will be to get a common locator available for your elements. Then we can use driver.findElements(By.cssSelector(locator)) to get list of all such elements and then loop through list to click all of them.
This way you no need to worry how many such elements are there in the page.
[1]: https://www.browserstack.com/guide/findelement-in-selenium

答案3

得分: 0

Sure, here's the translated code snippet:

将所有元素插入到 List<WebElement>并使用 for 循环获取每个元素

    List<WebElement> list = driver.findElements(By.cssSelector("[class=matchListWithMessages] div a"));
    
    for(WebElement row : list) {
         row.click();
    }

Please let me know if you need further assistance!

英文:

Insert all elements in to List<WebElement> and for loop it to get each element.

List&lt;WebElement&gt; list = driver.findElements(By.cssSelector(&quot;[class=matchListWithMessages] div a)&quot;));

for(WebElement row : list) {
     row.click();
}

huangapple
  • 本文由 发表于 2020年8月14日 19:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定