处理Selenium WebDriver中的弹出窗口不起作用 [英] Handling the popup window in Selenium WebDriver doesn't work

查看:347
本文介绍了处理Selenium WebDriver中的弹出窗口不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebDriver启动浏览器并导航到URL,然后单击应用程序中的链接,然后会出现一个带有弹出窗口的新浏览器,直到关闭弹出窗口,我们才能使用该浏览器进行任何控制.

WebDriver launches browser and navigates to the URL and clicks a link in the application, then a new browser with pop-up window appears and we can't get any control with the browser till we close the pop-up window.

弹出窗口只有确定"按钮.我已经尝试过switchTo(),窗口处理程序,但是不起作用.另外,由于此弹出窗口被阻止,因此无法获得浏览器的控制权.

The pop-up has only "OK" button. I have tried switchTo(), window handler, but doesn't work. Also, not able to get the control of the browser as this popup blocks.

推荐答案

切换到弹出窗口时,您必须提供窗口句柄,以便您可以控制发生的事情.我使用该类来使其更容易来回切换.

You have to provide the window handle when switching to the popup so you can control whats going on there. I use this class to make it easier to switch back and forth.

这在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 
}

然后在我的程序中执行以下操作:

then in my program I do this:

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);`

这篇关于处理Selenium WebDriver中的弹出窗口不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆