如何使用 selenium/java 切换到新的弹出窗口 window
How to switch to a new pop-up window using selenium/java
我需要自动执行以下测试:
在网页上登录后,一个新的弹出窗口 window(带有应用程序)打开,所有步骤都应在这个新的 window.
中完成
问题:
如何编码从当前登录 window 切换到新的弹出窗口 window?
谢谢!
如果你想处理 child window 那么你必须在 selenium 中使用句柄,请参考下面的代码:
String parentWindowHandle = driver.getWindowHandle(); // get the current window handle
//Perform action on your parent window
//Perform clcik() action on your parent window that opens a new window
for (String winHandle : driver.getWindowHandles()) {
if(!winHandle.equals(parentWindowHandle))
{
driver.switchTo().window(winHandle); // Here yor switching control to child window so that you can perform action on child window
System.out.println("Title of the new window: " +
driver.getTitle());
//code to do something on new window
System.out.println("Closing the new window...");
driver.close();
}
}
driver.switchTo().window(parentWindowHandle);
System.out.println("Parent window URL: " + driver.getCurrentUrl());
我需要自动执行以下测试: 在网页上登录后,一个新的弹出窗口 window(带有应用程序)打开,所有步骤都应在这个新的 window.
中完成问题: 如何编码从当前登录 window 切换到新的弹出窗口 window?
谢谢!
如果你想处理 child window 那么你必须在 selenium 中使用句柄,请参考下面的代码:
String parentWindowHandle = driver.getWindowHandle(); // get the current window handle
//Perform action on your parent window
//Perform clcik() action on your parent window that opens a new window
for (String winHandle : driver.getWindowHandles()) {
if(!winHandle.equals(parentWindowHandle))
{
driver.switchTo().window(winHandle); // Here yor switching control to child window so that you can perform action on child window
System.out.println("Title of the new window: " +
driver.getTitle());
//code to do something on new window
System.out.println("Closing the new window...");
driver.close();
}
}
driver.switchTo().window(parentWindowHandle);
System.out.println("Parent window URL: " + driver.getCurrentUrl());