12

谁能告诉我如何使用 webdriver 关闭除第一个选项卡/主选项卡之外的所有打开的选项卡?

我在下面尝试过,但它也关闭了所有选项卡,包括第一个选项卡。

public static void closeTabs() {
    String wh1=driver.getWindowHandle();
    String cwh=null;
    while(wh1!=cwh)
    {   
    new Actions(driver).sendKeys(Keys.CONTROL).sendKeys(Keys.NUMPAD1).perform();
    driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL, Keys.TAB);
    cwh=driver.getWindowHandle();
    driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"w");
    }
}

请帮我。

4

7 回答 7

29

获取所有窗口句柄,然后遍历它们,将 webdriver 切换到新句柄,然后调用 close 方法。显然跳过这个原始手柄,然后切换回剩余的手柄。

就像是;

    String originalHandle = driver.getWindowHandle();

    //Do something to open new tabs

    for(String handle : driver.getWindowHandles()) {
        if (!handle.equals(originalHandle)) {
            driver.switchTo().window(handle);
            driver.close();
        }
    }

    driver.switchTo().window(originalHandle);
于 2013-08-29T07:41:00.540 回答
1

试试这段代码,它应该可以工作:

for(String winHandle : driver.getWindowHandles())
  {
    if (winHandle == driver.getWindowHandles().toArray()[driver.getWindowHandles().size()-1])     
     {
      continue;
     }
    driver.switchTo().window(winHandle);
    driver.close();
  }
于 2013-08-29T08:13:33.543 回答
1

尝试这个:

for(int i = driver.getWindowHandles().size() -1 ; i > 0 ; i--){

        String winHandle = driver.getWindowHandles().toArray()[i].toString();

        driver.switchTo().window(winHandle);

        driver.close();

}
于 2016-10-14T03:27:09.323 回答
1

我做了以下操作来关闭除主窗口以外的所有窗口:

// Find out which handle is the one of the main window
String mainWindow = driver.CurrentWindowHandle;

// Get a list of all windows, except the main window
driver.WindowHandles.Where(w => w != mainWindow).ToList()
    // For each window found
    .ForEach(w =>
    {
        // switch to the window
        driver.SwitchTo().Window(w);

        // close the window
        driver.Close();
    });

// At the end, come back to the main window
driver.SwitchTo().Window(mainWindow);
于 2017-06-07T14:12:32.077 回答
0

I have an utility method to switch to the required window as shown below

public class Utility 
{
    public static WebDriver getHandleToWindow(String title){

        //parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
        System.err.println("No of windows :  " + windowIterator.size());
        for (String s : windowIterator) {
          String windowHandle = s; 
          popup = WebDriverInitialize.getDriver().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;
        }
}

It will take you to desired window once title of the window is passed as parameter. In your case you can do.

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
childDriver.close();

and then again switch to parent window using the same method

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

This method works effectively when dealing with multiple windows.

于 2013-08-29T09:06:59.873 回答
0

如果有人正在寻找 VBA SeleniumBasic ChromeDriver 解决方案,请看这里。

Dim TabA as String
TabA = MyChromeDriver.Windows.Item(1).Title
Do While MyChromeDriver.Windows.Count > 1
     MyChromeDriver.Windows.Item(MyChromeDriver.Windows.Count).Close
Loop
On Error Resume Next
     MyChromeDriver.SwitchToWindowByTitle TabA
On Error GoTo 0
于 2020-09-05T22:14:26.710 回答
0

这将为您提供清晰的图片,将按以下顺序排列:

  • 创建一个新选项卡
  • 切换到它
  • 在新标签中做一些事情
  • 关闭新标签
  • 切换回我们原来的标签
// save a reference to our original tab's window handle
var originalTabInstance = myWebDriverInstance.CurrentWindowHandle;

// execute some JavaScript to open a new window
myWebDriverInstance.ExecuteJavaScript("window.open();");

// save a reference to our new tab's window handle, this would be the last entry in the WindowHandles collection
var newTabInstance = myWebDriverInstance.WindowHandles[Driver.Instance.WindowHandles.Count - 1];

// switch our WebDriver to the new tab's window handle
myWebDriverInstance.SwitchTo().Window(newTabInstance);

// lets navigate to a web site in our new tab
myWebDriverInstance.Navigate().GoToUrl("www.crowbarsolutions.com");

// now lets close our new tab
myWebDriverInstance.ExecuteJavaScript("window.close();");

// and switch our WebDriver back to the original tab's window handle
myWebDriverInstance.SwitchTo().Window(originalTabInstance);

// and have our WebDriver focus on the main document in the page to send commands to myWebDriverInstance.SwitchTo().DefaultContent();
于 2021-01-05T16:56:33.733 回答