Does Selenium ChromeDriver currently have support for handling pop-up windows?

此生再无相见时 提交于 2019-12-24 01:17:55

问题


I have a pop-up window that I have to handle after clicking a button on the Chrome browser. As soon as the popup is launched programmatically, any further action by the driver object results in this exception:

OpenQA.Selenium.WebDriverException: No response from server for url

Also there is no change in number of windowhandles list (driver.WindowHandles) after the pop-up window is launched. Please don't confuse this with switching windows in tabs.

Does Selenium ChromeDriver currently have support for handling pop-up windows?


回答1:


Treating pop-up window is the same as treating number of windows. Try to do:

driver().switchTo().window((String)driver.getWindowHandles().toArray()[index]);

Where index is the index of the new window (can be passed as an argument in appropriate switch-window function).

Later, you can implement it in more clean way such as:

*In our case the webdriver is warped in WebDriverProxy object.

    public static void switchFocusToWindowNumber(int index, WebDriverProxy webDriverProxy) {
    try {
        webDriverProxy.getWebDriver().switchTo().window((String) webDriverProxy.getWebDriver().getWindowHandles().toArray()[index]);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException: Number of open windows is: "
                + getNumberofWindows(webDriverProxy) + " Trying to close window number: " + index + "\n Exception: " + e);
    }    catch (NoSuchWindowException e) {
        throw new NoSuchWindowException ("NoSuchWindowException: Number of windows is: " + getNumberofWindows(webDriverProxy) +
                " Trying to close window number: " + index + "\n Exception: " + e);
    }

}


来源:https://stackoverflow.com/questions/12450384/does-selenium-chromedriver-currently-have-support-for-handling-pop-up-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!