简体   繁体   English

单击列表中找到的所有元素<WebElement>

[英]Clicking all elements found in List<WebElement>

I am trying to click all of the instances of an element found by the class attribute found in an i tag within an anchor tag in the row of a dynamic table.我正在尝试单击动态表行中锚标记内的 i 标记中的 class 属性找到的元素的所有实例。 Specifically, its a row with several cells and in the last cell there is a series of 4 links with either and X or an O. If the square is an X, it can be clicked and after a short wait, changes to an O. The links are written as follows:具体来说,它是包含多个单元格的一行,在最后一个单元格中有一系列 4 个链接,带有 X 或 O。如果正方形是 X,则可以单击它,并在短暂等待后变为 O。链接写成如下:

<a class="foobar"><i class="dontNeedToClick"><i></a>
<a class="foobar"><i class="dontNeedToClick"><i></a>
<a class="foobar"><i class="needToClick"><i></a>
<a class="foobar"><i class="dontNeedToClick"><i></a>

When the needToClick element is clicked, the class would change to a dontNeedToClick element There are less than a hundred X's in a database of thousands of O's to scroll through, so something to automate clicking them would save a lot of time.当needToClick 元素被单击时,该类将更改为dontNeedToClick 元素。在包含数千个O 的数据库中,只有不到一百个X 可供滚动,因此自动单击它们会节省大量时间。 I can successfully count the instances the element appears in the table, but when I put the elements into a List object using .findElements() and attempt to iterate through it, I get ElementNotVisibleException.我可以成功计算元素出现在表中的实例,但是当我使用 .findElements() 将元素放入 List 对象并尝试遍历它时,我得到 ElementNotVisibleException。

Here is my sample code so far:到目前为止,这是我的示例代码:

// get the count of thumbs down accounts
    int elementsCount= driver.findElements(By.className("needToClick")).size();
    System.out.println("Amount of elements: " + elementsCount);

// loop and click through all elements found

    for (int x = 0; x < elementCount; x++) {
        List<WebElement> elements = driver.findElements(By.className("needToClick"));
        WebElement client = elements.get(x);
        client.click();
        Thread.sleep(5000);
    }

I have tried using Iterator and get the same exception thrown at me.我试过使用 Iterator 并得到同样的异常。

        Iterator<WebElement> clickElement = elements.iterator();
        while (clickElement.hasNext()) {
            clickElement.next().click();
        }

I have also tried using a foreach loop.我也尝试过使用 foreach 循环。

    List<WebElement> thumbsDown = driver.findElements(By.className(expectedClass));

    for(WebElement element : thumbsDown) {
        element.click();
    }

I would use xpath but I am not very experienced with dynamic tables and haven't been able to find an approach that would fetch the row containing the element I'm looking for and other elements relative to it and figured a less than elegant approach such as just finding all the elements by the class name would suffice.我会使用 xpath,但我对动态表的经验不是很丰富,并且无法找到一种方法来获取包含我正在寻找的元素的行以及与它相关的其他元素,并且想出了一种不太优雅的方法,例如因为只需通过类名查找所有元素就足够了。 Would appreciate any help.将不胜感激任何帮助。

Make sure elements you try to click is visible.确保您尝试单击的元素可见。 If you don't care about visibility click with javascipt:如果您不关心可见性,请使用 javascipt 单击:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, 5);

List<WebElement> elements = driver.findElements(By.className("needToClick"));
elements.forEach(e -> {
    js.executeScript("arguments[0].click();", e);
    wait.until(ExpectedConditions.attributeToBe(e,"class", "dontNeedToClick"));
    //if element have not only dontNeedToClick class use attributeContains
    //wait.until(ExpectedConditions.attributeContains(e,"class", "dontNeedToClick"));
});

Check if you can click one element and class changes:检查您是否可以单击一个元素并更改类:

driver.findElement(By.className("needToClick")).click();

if not try xpath:如果不尝试 xpath:

driver.findElement(By.xpath("//a[i[@class='needToClick']]"));
//or
driver.findElement(By.xpath("//a[i[contains(@class,'needToClick')]]"));

Is the exception being thrown when you iterate through the list, or when you attempt to generate the list?是否在遍历列表或尝试生成列表时抛出异常?

If it's the former, what you need to do is re-generate the list after every click - even if you load a new page that's exactly the same, selenium will see the elements on the new page as 'different' and you won't be able to use any of the elements you found on the old page.如果是前者,您需要做的是在每次点击后重新生成列表 - 即使您加载了一个完全相同的新页面,硒也会将新页面上的元素视为“不同”而您不会能够使用您在旧页面上找到的任何元素。 (Edit: I looked through your loop again, and it seems like you're doing this part right. My guess would be you need to wait for the page to load fully) (编辑:我再次查看了你的循环,看起来你做的这部分是对的。我猜你需要等待页面完全加载)

If it's the latter, then you might need to wait for the page to load, and make sure that the elements you're trying to find all exist.如果是后者,那么您可能需要等待页面加载,并确保您尝试查找的元素都存在。 In selenium you can set explicit or implicit timeouts, but I personally prefer time.sleep(however many seconds) since selenium might 'say' it's loaded before fully loading all the javascript.在 selenium 中,您可以设置显式或隐式超时,但我个人更喜欢time.sleep(however many seconds)因为 selenium 可能会“说”它在完全加载所有 javascript 之前已加载。

Hope this helps!希望这可以帮助!

I think you have to use xpath, as I saw your element in <i> tags我认为您必须使用 xpath,因为我在<i>标签中看到了您的元素

like this像这样

//a[@class='foobar']//i[contains(@class,'Click')]

Code代码

 List<WebElement> elements = driver.findElements(By.xpath("//a[@class='foobar']//i[contains(@class,'Click')]"));
 for (WebElement tempEle : elements) {
  tempEle.click();
}

You can also use if with break in the For each loop to seelct particular element您还可以在 For each 循环中使用if with break来查找特定元素

If you want to select every Element of a List List just use a foreach-loop:如果要选择列表列表的每个元素,只需使用 foreach 循环:

for(WebElement e : List<WebElement>){
   e.doThings();
}
   int elementsCount= driver.findElements(By.className("needToClick")).size();
System.out.println("Amount of elements: " + elementsCount);

    List<WebElement> elements = driver.findElements(By.className("needToClick"));

for (int x = 0; x < elementCount; x++) {
    WebElement client = elements.get(x);
    Thread.sleep(5000);

client.click();客户端点击(); } }

List elements = driver.findElements(By.className("needToClick"));列表元素 = driver.findElements(By.className("needToClick")); elements.get(0).click(); element.get(0).click(); It will work for the click event on web list Elements它将适用于网络列表元素上的点击事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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