简体   繁体   English

在Selenium Webdriver中循环运行Java代码

[英]Run Java code in loop in Selenium Webdriver

I have two button with text "x" and they both have same cssSelector a.aui-button.aui-button-link.aui-restfultable-delete.aui-restfultable-delete-small 我有两个带有文本“ x”的按钮,它们都有相同的cssSelector a.aui-button.aui-button-link.aui-restfultable-delete.aui-restfultable-delete-small

The code below is able to click on the first "x", however fails because there is a pop up window asking for confirm to delete. 下面的代码可以单击第一个“ x”,但是失败,因为有一个弹出窗口要求确认删除。

I want the code to click on first "x" and than click on "Delete" to confirm on the pop up window and repeat for the second "x". 我希望代码单击第一个“ x”,然后单击“删除”以在弹出窗口中确认并重复第二个“ x”。 The cssSelector for "Delete" in Pop up window is aui-button.js-confirm-action-button 弹出窗口中用于“删除”的cssSelector是aui-button.js-confirm-action-button

Eg (Screenshot) 例如(截屏)

//Delete all existing Request Types
    List<WebElement> allele = driver.findElements(By.cssSelector("a.aui-button.aui-button-link.aui-restfultable-delete.aui-restfultable-delete-small"));

    // Looping through the elements using for-each loop
    for(WebElement eachEle : allele) {
        // Checking whether the element is enabled or not
        if(eachEle.isEnabled()) {
            eachEle.click();
        }
    }

Any Suggestion how can I write code to do this? 任何建议我该如何编写代码来做到这一点? Check Image here to get an idea of what I am trying to do Example Image 在此处查看“图像”以了解我正在尝试做的示例图像

Modify the piece of code as shown below: 修改这段代码,如下所示:

// Initializing the webdriver wait
WebDriverWait wait = new WebDriverWait(driver, 10);

//Delete all existing Request Types
List<WebElement> allele = driver.findElements(By.cssSelector("a.aui-button.aui-button-link.aui-restfultable-delete.aui-restfultable-delete-small"));

// Looping through the elements using for-each loop
for(WebElement eachEle : allele) {
    // Checking whether the element is enabled or not
    if(eachEle.isEnabled()) {
        eachEle.click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();  
    }
}

Hope this helps. 希望这可以帮助。

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

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