gpt4 book ai didi

java - 如何使用selenium/java切换到新的弹出窗口

转载 作者:行者123 更新时间:2023-12-02 08:45:08 26 4
gpt4 key购买 nike

我需要自动化以下测试:在网页上登录后,将打开一个新的弹出窗口(带有应用程序),所有步骤都应在这个新窗口中完成。

问题:如何编写代码从当前登录窗口切换到新的弹出窗口?

谢谢!

最佳答案

如果你想处理子窗口,那么你必须使用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());

关于java - 如何使用selenium/java切换到新的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com