英文:
How to switch to a new pop-up window using selenium/java
问题
我需要自动化执行以下测试:
在网页登录后,会弹出一个新的应用程序弹窗,所有步骤都应在这个新弹窗中完成。
问题:
如何编写代码以从当前登录窗口切换到新的弹出窗口?
谢谢!
英文:
I need to automate the following test:
after the login on the web page a new pop-up window (with app) opens and all steps should be done in this new window.
Question:
how to code to switch from the current login window to the new pop-up window?
Thank you!
答案1
得分: 1
如果您想处理子窗口,您需要在Selenium中使用句柄。请参考以下代码:
String parentWindowHandle = driver.getWindowHandle(); // 获取当前窗口句柄
// 在父窗口上执行操作
// 在父窗口上执行click()操作,打开一个新窗口
for (String winHandle : driver.getWindowHandles()) {
if (!winHandle.equals(parentWindowHandle)) {
driver.switchTo().window(winHandle); // 在这里切换到子窗口,以便在子窗口上执行操作
System.out.println("新窗口的标题: " + driver.getTitle());
// 在新窗口上执行某些操作的代码
System.out.println("关闭新窗口...");
driver.close();
}
}
driver.switchTo().window(parentWindowHandle);
System.out.println("父窗口的URL: " + driver.getCurrentUrl());
英文:
If you want to handle child window then you have to use handles in seleneium, Kindly refer below code:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论