0

我有一个要求,我从站点获取文本,然后在循环中使用它,以便它可以继续单击基于该变量的元素。这有效,但只能循环 5 次,它应该点击的次数是 100 次或以上。这是代码示例

String vText= driver.findElement(By.xpath(")).getText();
System.out.println(vText);
int vEle= vText.length(); 
for (int i=0; i<vEle; i++){
   driver.findElement(By.xpath("")).click(); 

我做错了什么请帮帮我

谢谢, 梅迪哈

4

3 回答 3

2

您可能需要 100 次验证您从中获取文本的定位器。如果所有 100 个元素都相同,它将起作用。

您是否尝试过findElements方法?

//It will return the List of webelements which has same locator
List<WebElement> elements=driver.findElements(By.xpath(""));

//Now iterate through List and do the required operation with each individual element
for(WebElement ele:element)
{
    ele.getText();   //It will print innertext of each element
    ele.click();     //It will click on each element
}
于 2013-07-18T04:39:44.643 回答
0

感谢 Santosh 和 Pavel,

我实际上找到了另一种方法,尽管我会查看您发送给我 Santosh 的代码。但这是我想出的代码,请给我评论,看看这是否是最好的方法。

String vText driver.findElement(By.xpath("")).getText();

//This will split the output from the slash and the output looks like 1/120
final String[] splitted = vText.split("\\D+");

//Here it will parse that output to an integer type and start with the first location
final int slideCount = Integer.parseInt(splitted[1]);

for (int i=0; i<slideCount; i++) {
   driver.findElement(By.xpath("")).click(); 
}   
于 2013-07-18T13:16:08.600 回答
0

我认为您的示例中的错误在这一行:

int vEle= vText.length(); 

后来你的循环是:

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

这意味着你的循环只会在文本很长时发生那么多次。因此,如果文本是Hello那么循环只会发生 5 次。

于 2013-07-18T07:31:59.513 回答