Selenium WebDriver for C# - Popup Dialog Boxes

2019-04-15 10:24发布

Is there any support for working with popup dialogs (specifically file downloads) in c#?

3条回答
Anthone
2楼-- · 2019-04-15 11:02

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;
            }
        }
查看更多
beautiful°
3楼-- · 2019-04-15 11:03

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.

查看更多
Bombasti
4楼-- · 2019-04-15 11:04

For a popup windows dialog, you can use the alert to catch:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
查看更多
登录 后发表回答