如何使用Selenium WebDriver with Java关闭特定窗口?

20jt8wwn  于 2023-01-15  发布在  Java
关注(0)|答案(5)|浏览(157)

我使用Selenium WebDriver。我打开第一个页面,然后打开第二个页面-执行一些操作,然后返回到第一个页面。在我想关闭第二个页面之前,我使用命令driver.close();,但它关闭的是第一个页面而不是第二个页面。我如何让Selenium关闭特定的窗口?
代码的一部分

String HandleBefore = driver.getWindowHandle();

 driver.findElement(By.xpath("...")).click();
 for (String twohandle : driver.getWindowHandles()) {
        driver.switchTo().window(twohandle);
    }       
 driver.findElement(By.linkText("001")).click();
 driver.close();
vlf7wbxs

vlf7wbxs1#

String base = driver.getWindowHandle();

    Set <String> set = driver.getWindowHandles();

    set.remove(base);
    assert set.size()==1;

    driver.switchTo().window(set.toArray(new String[0]));

    driver.close();
    driver.switchTo().window(base);

这对我有用...

bxgwgixi

bxgwgixi2#

在Python中

default_handle = driver.current_window_handle
handles = list(driver.window_handles)
assert len(handles) > 1

handles.remove(default_handle)
assert len(handles) > 0

driver.switch_to_window(handles[0])
# do your stuffs
driver.close()
driver.switch_to_window(default_handle)
8ulbf1ek

8ulbf1ek3#

您可以通过窗口标题或标识该窗口的特定唯一元素来关闭特定窗口。

private void SwitchTabandClose()
{
    Set <String> windows = driver.getWindowHandles();
    String mainwindow = driver.getWindowHandle();

    for (String handle: windows)
    {
        driver.switchTo().window(handle);
        System.out.println("switched to "+driver.getTitle()+"  Window");
        String pagetitle = driver.getTitle();
        if(pagetitle.equalsIgnoreCase("XYZ Title"))
        {
            driver.close();
            System.out.println("Closed the  '"+pagetitle+"' Tab now ...");
        }
    }

    driver.switchTo().window(mainwindow);
 }
ckocjqey

ckocjqey4#

Ramnarayan的解决方案效果很好,为了可重用性做了一些修改:

public static String switchToWindowByTitle(WebDriver driver, String windowTitle) {
  Set<String> handles = driver.getWindowHandles();
  String currentHandle = driver.getWindowHandle();
  for (String handle : handles) {
    driver.switchTo().window(handle);
    if (windowTitle.equalsIgnoreCase(driver.getTitle())) {
      break;
    }
  }

  return currentHandle;
}

// How to use method
String currentHandle = SeleniumUtil.switchToWindowByTitle(driver, WINDOW_TITLE);
driver.close(); // Close WINDOW_TITLE page or do other things
driver.switchTo().window(currentHandle); // Return to current page
aurhwmvo

aurhwmvo5#

在其他岗位工作的一点后续,我已经这样做了。

public static void changeFocusToWindowByTitle(WebDriver driver, String windowTitle) {
        Set<String> handles = driver.getWindowHandles();
        for (String windowHandle : handles) {
            driver.switchTo().window(windowHandle);
            if (windowTitle.equalsIgnoreCase(driver.getTitle())) {
                logger.debug("Focus has been changed to the window: " + driver.getTitle());
                break;
            }
        }
    }

 public static void closeWindow(WebDriver driver, String windowTitle) {
        Set<String> handles = driver.getWindowHandles(); ;
        for (String handle: handles ) {
            driver.switchTo().window(handle);
            if (driver.getTitle().equalsIgnoreCase(windowTitle)) {
                logger.debug("Close window: " + windowTitle);
                driver.close();
                break;
            }
        }
    }

相关问题