gpt4 book ai didi

java - 如何在使用java关闭Selenium Automation中的当前窗口后切换到现有窗口

转载 作者:行者123 更新时间:2023-11-28 21:31:35 26 4
gpt4 key购买 nike

实际上,在使用 java 关闭 Selenium Automation 中的当前窗口后,我试图将控件从当前窗口切换到现有窗口。有什么办法可以做到这一点。我能够控制新打开的窗口,进行一些处理并关闭这个窗口。稍后我只需要移动到现有的浏览器窗口即可。

最佳答案

这是我使用的东西,它会检查所有打开的窗口,然后将控制权切换到下一个窗口,并提供关闭旧窗口的选项。

protected final void switchWindows(boolean closeOldWindow) {
final WebDriver driver = checkNotNull(getDriver(), "missing WebDriver");

final String currentWindow = driver.getWindowHandle();
checkNotNull(currentWindow);

// switch to first window that is not equal to the current window
String newWindow = null;
for (final String handle : driver.getWindowHandles()) {
if (!currentWindow.equals(handle)) {
newWindow = handle;
break;
}
}

// if there's another window found...
if (newWindow != null) {
if (closeOldWindow) {
// close the current window
driver.close();
}
// ...switch to the new window
driver.switchTo().window(newWindow);
}
}

关于java - 如何在使用java关闭Selenium Automation中的当前窗口后切换到现有窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520244/

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