首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >关闭selenium java中的弹出窗口

关闭selenium java中的弹出窗口
EN

Stack Overflow用户
提问于 2018-05-31 20:50:01
回答 1查看 257关注 0票数 1

我希望使用selenium关闭我父母窗口上的弹出窗口。我正在使用java。

到目前为止,我正在遍历页面上的所有DIVs,直到看到" close“文本(带有关闭文本的按钮),然后如果我找到close,则单击close按钮,但这需要时间,因为它遍历所有DIVs。

当前代码:

代码语言:javascript
复制
// Find the visible element that has text 'Close' and click on it
WebElement closeButton = Functional
        .getVisibleElement(pDriver.findElements(By.xpath("//span[contains(.,'Close')]")));
if (closeButton != null) {
    closeButton.click();


public static WebElement getVisibleElement(List<WebElement> pListOfElements) {
    for (int i = 0; i < pListOfElements.size(); i++) {
        if (pListOfElements.get(i).isDisplayed()) {
            for (int a = 0; a <= ATTEMPT; a++) {
                try {
                    return pListOfElements.get(i);
                } catch (StaleElementReferenceException e) {
                    LOGGER.info("attempting to press the element. Amount of attempt: " + ATTEMPT);
                }
            }
        } else {

        }
    }
    return null;
}

有没有什么方法可以在selenium中关闭弹出窗口。

EN

回答 1

Stack Overflow用户

发布于 2018-06-01 04:16:38

如果您希望接受警报弹出窗口并根据需要传递超时,请尝试此方法(示例: 4000 (4秒))

代码语言:javascript
复制
    public static void acceptAlertIfAvailable(long timeout) {
    //log.info("Checking if there is a popup alert to accept it");
    long waitForAlert = System.currentTimeMillis() + timeout;
    boolean boolFound = false;
    do {
        try {
            Alert alert = driver.switchTo().alert();
            if (alert != null) {
                String alertMsg = alert.getText();
                alert.accept();
                //log.info("Pop-up alert [" + alertMsg + "] was found and accepted");
                boolFound = true;
            }
        } catch (NoAlertPresentException ex) {
            //log.error("Pop-up alert Not handled and Exception occured --> " + ex.getMessage());
        }
    } while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}

如果您想要解除警报,请尝试此方法:

代码语言:javascript
复制
    public static void dismissAlertIfAvailable(long timeout) {
    //log.info("Checking if there is a popup alert to dismiss it");
    long waitForAlert = System.currentTimeMillis() + timeout;
    boolean boolFound = false;
    do {
        try {
            Alert alert = driver.switchTo().alert();
            if (alert != null) {
                String alertMsg = alert.getText();
                alert.dismiss();
                //log.info("Pop-up alert [" + alertMsg + "] was found and dismissed");
                boolFound = true;
            }
        } catch (NoAlertPresentException ex) {
            //log.error("Pop-up alert Not handled and Exception occured --> " + ex.getMessage());
        }
    } while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50624770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档