WebDriver启动浏览器并导航到URL并单击应用程序中的链接,然后会出现一个带有弹出窗口的新浏览器,在关闭弹出窗口之前我们无法控制浏览器。
弹出窗口只有“确定”按钮。我试过switchTo()
,窗口处理程序,但不起作用。此外,由于此弹出窗口阻止,无法控制浏览器。
答案 0 :(得分: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);`