-1

WebDriver 启动浏览器并导航到 URL 并单击应用程序中的链接,然后出现一个带有弹出窗口的新浏览器,在我们关闭弹出窗口之前我们无法使用浏览器进行任何控制。

弹出窗口只有“确定”按钮。我已经尝试过switchTo(),窗口处理程序,但不起作用。此外,由于此弹出窗口阻塞,无法控制浏览器。

4

1 回答 1

1

切换到弹出窗口时,您必须提供窗口句柄,以便控制那里发生的事情。我使用这个类来更容易地来回切换。

这是在 C# 中:

public class WindowManager
{
    private string _parentWindowHandle;
    private string _popupWindowHandle;

    public void SwitchWindowFocusToPopup(IWebDriver driver, string NewWindowTitle)
    {        
        //pass the expected popup window title so the IWebDriver can get 
        //the windowhandle and assign it to the current iWebDriver

        IWebDriver popup = null;

        var windowIterator = driver.WindowHandles;

        foreach (var windowHandle in windowIterator)
        {
            popup = driver.SwitchTo().Window(windowHandle);
            if (popup.Title == NewWindowTitle)
            {
                _popupWindowHandle = popup.CurrentWindowHandle;
                break;
            }
        }

    }

    #region Properties

    public string ParentWindowHandle
    {
        get
        {
            return _parentWindowHandle;
        }
        set
        {
            _parentWindowHandle = value;
        }
    }

    public string PopupWindowHandle
    {
        get
        {
            return _popupWindowHandle;
        }
        set
        {
            _popupWindowHandle = value;
        }

    }
    #endregion 
}

然后在我的程序中我这样做:

WindowManager windowManager = new WindowManager();
windowManager.ParentWindowHandle = driver.CurrentWindowHandle;
//do stuff that opens the new window
//immediately switch focus to the popup so webdriver can work with the page
windowManager.SwitchWindowFocusToPopup(driver, "popup window title");

//do stuff with the popup

//close the popup
driver.Close();

//set the webdriver window back to the original parent window
driver.SwitchTo().Window(windowManager.ParentWindowHandle);`
于 2013-08-15T23:10:25.887 回答