gpt4 book ai didi

java - 自动关闭窗口并切换回 Selenium 中的父窗口

转载 作者:行者123 更新时间:2023-11-29 04:52:32 25 4
gpt4 key购买 nike

基本 我正在使用带 POM 的 maven,在第二个窗口中执行操作并单击发送窗口自动关闭后,我无法将焦点切换回父窗口,之后我的 Firefox 驱动程序不起作用。

**我使用了下面提到的三个选项,但它们不起作用**

    public void switchWindow() {
String handle = driver.getWindowHandle();
Set<String> set = driver.getWindowHandles();
set.remove(handle);
if (set.size() >= 1) {
driver.switchTo().window((String) set.toArray()[0]);
}


public WebDriver switchWindowByTitle(String title)
{
WebDriver popup = null;
Set<String> windowIterator = driver.getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = driver.switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}

}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;


public WebDriver switchWindowByTitle(String title)
{

String currentWindow = driver.getWindowHandle();
List<String> availableWindows = new ArrayList<String>(driver.getWindowHandles());

for(String w : availableWindows)
{
if (w != currentWindow)
{
driver.switchTo().window(w);
if (driver.getTitle().equalsIgnoreCase(title))
return true;
else
{
driver.switchTo().window(currentWindow);
}
}
}
return false;
}

最佳答案

您需要在打开新窗口之前获取父窗口句柄,并在完成新窗口后切换回父窗口句柄。

// before opening the new window
String parentWindow = driver.getWindowHandle();

// after the new window was closed
driver.switchTo().window(parentWindow);

关于java - 自动关闭窗口并切换回 Selenium 中的父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883532/

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