【问题标题】:close popup in selenium java在 selenium java 中关闭弹出窗口
【发布时间】:2018-06-01 04:16:37
【问题描述】:

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

到目前为止,我正在循环浏览页面上的所有 DIV,直到我看到“关闭”文本(带有关闭文本的按钮),然后如果我找到关闭,然后单击关闭按钮但是这需要时间,因为它会遍历所有 DIV .

当前代码:

// 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 中的弹出窗口。

【问题讨论】:

  • 看起来像是一个 javascript 弹出窗口而不是一个新窗口?如果一次只有一个弹出窗口,也许有一个更容易找到的定位器。你能添加相关的html吗?
  • 不是警报之类的 java 脚本窗口。它是来自服务器的响应,显示在窗口中。

标签: java selenium


【解决方案1】:

如果您想接受弹出警报并根据需要通过超时,请尝试此方法(例如:4000(其 4 秒))

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

如果您想解除警报,请尝试以下方法:

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

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 1970-01-01
    • 2017-06-04
    • 2019-10-22
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多