问题
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