我有两个带有文本“ x”的按钮,它们都有相同的cssSelector a.aui-button.aui-button-link.aui-restfultable-delete.aui-restfultable-delete-small

下面的代码可以单击第一个“ x”,但是失败,因为有一个弹出窗口要求确认删除。

我希望代码单击第一个“ x”,然后单击“删除”以在弹出窗口中确认并重复第二个“ x”。弹出窗口中用于“删除”的cssSelector为aui-button.js-confirm-action-button

例如。 (截图)

//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();
        }
    }


任何建议我该如何编写代码来做到这一点?在此处查看图片以了解我要做什么Example Image

最佳答案

修改这段代码,如下所示:

// 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();
    }
}


希望这可以帮助。

10-07 23:51