sodcalculator

10 Tech How-To

Close Browser Tab in Selenium: Step-by-Step

Guide 10-60351 · Updated · Step by step

Closing a browser tab in Selenium is a common task when automating multi-tab workflows, such as verifying a pop-up, checking a link that opens in a new tab, or cleaning up after a test. This guide shows you how to switch to a specific tab and close it reliably using Selenium WebDriver. You’ll need a working Selenium setup with a WebDriver for your browser (Chrome, Firefox, Edge, etc.) and a basic understanding of writing test scripts.

What You’ll Need

  • Selenium WebDriver installed (e.g., via pip for Python, Maven for Java, npm for JavaScript).
  • A browser driver (chromedriver, geckodriver, msedgedriver) matching your browser version.
  • An IDE or text editor to write your script.
  • Basic knowledge of your chosen programming language’s syntax.

Step-by-Step

  1. Launch the browser and open a page that has multiple tabs. For example, navigate to a page that opens a new tab when you click a link. This gives you at least two tabs to work with.
  2. Get the list of open window handles. Use the WebDriver method to fetch all window handles. In Python: handles = driver.window_handles; in Java: Set<String> handles = driver.getWindowHandles(). This returns a set/array of unique IDs for each open tab.
  3. Switch to the tab you want to close. You need to bring that tab into focus before closing it. If you want to close the most recently opened tab, switch to the last handle in the set. In Python: driver.switch_to.window(handles[-1]). In Java, since it’s a Set, convert it to a list first: List<String> handleList = new ArrayList<>(handles); driver.switchTo().window(handleList.get(handleList.size()-1));
  4. Close the tab. Call the driver.close() method. This closes the current tab, not the entire browser. If you only have one tab open, it will close the browser.
  5. Switch back to the remaining tab (if needed). After closing, the WebDriver’s focus is lost. You must switch to one of the remaining handles. Re-fetch the window handles (they’ve changed) and switch to the first one. In Python: handles = driver.window_handles; driver.switch_to.window(handles[0]).
  6. Continue with your test. Now you can interact with the remaining tab as usual.

Common Mistakes

  • Using driver.quit() instead of driver.close(). quit() closes the entire browser session, not just one tab. This is the most frequent error.
  • Not switching to the tab before closing. If you call close() while the driver is focused on another tab, it will close the wrong tab – or the only tab, ending the session.
  • Assuming window handles are in order. The order of handles is not guaranteed to be the order tabs were opened. Always identify the correct handle by title or URL if you have more than two tabs.
  • Forgetting to switch back after closing. If you try to interact with the browser after closing a tab without switching, you’ll get a NoSuchWindowException.

Tips

  • Use a helper method. Write a function that takes a tab title or URL and closes that specific tab. This makes your code reusable and less error-prone.
  • Wait for the new tab to open. If you’re closing a tab that was opened by a click, ensure the new tab is fully loaded before trying to close it. Use an explicit wait for the number of window handles to increase.
  • Use driver.getWindowHandles() after closing. Always refresh the handle list after a close operation – the old handles are stale.
  • Consider using driver.switchTo().defaultContent() if you’re inside frames. This ensures you’re back at the top-level window before switching tabs.

Troubleshooting

Problem: NoSuchWindowException when trying to switch tabs.
Solution: This usually means the handle you’re using is outdated. Re-fetch the window handles right before switching.

Problem: driver.close() closes the whole browser.
Solution: You likely had only one tab open, or you didn’t switch to the tab you intended. Check that you’ve switched to a tab that is not the last remaining one.

Problem: The new tab doesn’t open when you click a link.
Solution: Some links open in a new tab only with a specific key combination (e.g., Ctrl+Click). In Selenium, you may need to send a key down to the element: element.sendKeys(Keys.CONTROL, Keys.ENTER) or use JavaScript to open the URL in a new tab.

Problem: The tab closes but the test hangs.
Solution: After closing, always switch to a remaining tab. If you don’t, the WebDriver may be waiting for an action on the closed tab. Use an explicit wait after switching back.

Frequently Asked Questions

What is the difference between close() and quit() in Selenium?

close() closes the currently focused tab/window. quit() closes the entire browser session and all its tabs. Use close() when you want to close one tab and keep the browser running.

How do I close a tab by its title?

Loop through all window handles, switch to each one, check the title using driver.title, and if it matches, call close(). Then break out of the loop and switch to a remaining handle.

Can I close a tab without switching to it first?

No, Selenium WebDriver does not provide a direct method to close a specific tab by handle without switching to it. You must switch to the tab first, then close it.

What happens if I close the last open tab?

If you close the only tab, the browser session ends, and the WebDriver becomes invalid. You’ll need to create a new driver instance to continue.

How do I handle multiple tabs that open automatically?

Use a loop to iterate through all window handles. For each handle, switch to it, check the URL or title, and close it if it matches your criteria. Then switch back to the main tab.

Does close() work in all browsers equally?

Yes, the close() method is part of the WebDriver W3C standard and works consistently across Chrome, Firefox, Edge, and Safari. However, the order of window handles may vary, so always use the handle list dynamically.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →