0
0
Selenium-pythonHow-ToBeginner ยท 4 min read

How to Close a Tab in Selenium WebDriver

In Selenium, you can close the current browser tab using the driver.close() method. To close a specific tab, switch to it first with driver.switch_to.window() and then call driver.close().
๐Ÿ“

Syntax

The main method to close a tab in Selenium is driver.close(). This closes the currently active tab or window. To close a specific tab, you first switch to it using driver.switch_to.window(window_handle), where window_handle is the unique identifier of the tab.

  • driver.close(): Closes the current tab or window.
  • driver.switch_to.window(handle): Switches focus to the tab/window identified by handle.
python
driver.switch_to.window(window_handle)
driver.close()
๐Ÿ’ป

Example

This example opens two tabs, switches to the second tab, closes it, and then switches back to the first tab.

python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Initialize the Chrome driver
driver = webdriver.Chrome()

# Open first tab
driver.get('https://example.com')

# Open a new tab using JavaScript
driver.execute_script('window.open("https://www.python.org", "_blank");')

# Get list of all window handles (tabs)
handles = driver.window_handles

# Switch to second tab
driver.switch_to.window(handles[1])
print('Current tab title:', driver.title)  # Should print Python's page title

# Close the second tab
driver.close()

# Switch back to first tab
driver.switch_to.window(handles[0])
print('Back to tab title:', driver.title)  # Should print Example Domain

# Close the browser
driver.quit()
Output
Current tab title: Welcome to Python.org Back to tab title: Example Domain
โš ๏ธ

Common Pitfalls

  • Not switching to the tab before closing: Calling driver.close() without switching closes the current tab, which may not be the intended one.
  • Using driver.quit() instead of driver.close(): driver.quit() closes all tabs and ends the session, not just one tab.
  • Not handling window handles properly: Always get the latest driver.window_handles list after opening new tabs.
python
## Wrong way: closes current tab without switching
# driver.close()

## Right way: switch to desired tab before closing
# driver.switch_to.window(window_handle)
# driver.close()
๐Ÿ“Š

Quick Reference

ActionMethodDescription
Close current tabdriver.close()Closes the active tab or window.
Switch to tabdriver.switch_to.window(handle)Switches focus to the tab/window identified by handle.
Close all tabs and quitdriver.quit()Closes all tabs and ends the WebDriver session.
โœ…

Key Takeaways

Use driver.close() to close the currently active tab in Selenium.
Always switch to the tab you want to close using driver.switch_to.window() before calling driver.close().
driver.quit() closes all tabs and ends the browser session, not just one tab.
Keep track of window handles with driver.window_handles to manage multiple tabs.
After closing a tab, switch to a valid open tab to continue automation.