问题
Is there any support for working with popup dialogs (specifically file downloads) in c#?
回答1:
For a popup windows dialog, you can use the alert to catch:
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
回答2:
No, there isn't - at least not natively.
WebDriver only interacts with the webpage. A popup dialog, once instantiated, becomes the domain of the operating system instead of the webpage.
You can circumvent the file download/upload dialog by issuing a POST or a GET with the content you are retrieving or sending to the server.
You can use tools such as AutoIt, or the Windows Automation API, to interact with other dialog windows.
回答3:
From the WebDriver FAQ: WebDriver offers the ability to cope with multiple windows. This is done by using the "WebDriver.switchTo().window()" method to switch to a window with a known name. If the name is not known, you can use "WebDriver.getWindowHandles()" to obtain a list of known windows. You may pass the handle to "switchTo().window()".
Full FAQ here.
Example from Thoughtworks
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
if (popup.getTitle().equals("Google") {
break;
}
}
Below is the example converted from Java into C# (with deprecated methods replaced)
String parentWindowHandle = _browser.CurrentWindowHandle; // save the current window handle.
IWebDriver popup = null;
var windowIterator = _browser.WindowHandles;
foreach (var windowHandle in windowIterator)
{
popup = _browser.SwitchTo().Window(windowHandle);
if (popup.Title == "Google")
{
break;
}
}
来源:https://stackoverflow.com/questions/10590803/selenium-webdriver-for-c-sharp-popup-dialog-boxes