Close all open tabs except first tab / main tab using webdriver

Can someone tell me how to close all open tabs except the first tab / main tab using webdriver?

I tried below, but it closes all tabs, including the first tab.

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"); } } 

Please help me.

+8
java selenium selenium-webdriver webdriver tabs
source share
5 answers

Get all the window handles, then go through them by switching the webdriver to the new handle, then call the close method. Obviously, skip this for the original handle, then go back to the remaining handle.

Something like:

  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); 
+17
source share

Try using this code:

 for(String winHandle : driver.getWindowHandles()) { if (winHandle == driver.getWindowHandles().toArray()[driver.getWindowHandles().size()-1]) { continue; } driver.switchTo().window(winHandle); driver.close(); } 
+1
source share

I did the following to close all windows except the main one:

 // 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); 
+1
source share

I have a utility 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; } } 

This will result in the desired window when the window title is passed as a parameter. In your case you can do.

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

and then switch back to the parent window using the same method

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

This method works effectively when working with multiple windows.

0
source share

Try the following:

 for(int i = driver.getWindowHandles().size() -1 ; i > 0 ; i--){ String winHandle = driver.getWindowHandles().toArray()[i].toString(); driver.switchTo().window(winHandle); driver.close(); } 
0
source share

All Articles