- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to close active/current tab without closing the browser in Selenium-Python?
We can close the active/current tab without closing the browser in Selenium webdriver in Python. By default, Selenium has control over the parent window. Once another browser window is opened, we have to explicitly shift the control with the help of switch_to.window method.
The handle id of the browser window where we want to shift is passed as a parameter to that method. The method window_handles returns the list of all window handle ids of the opened browsers.
The method current_window_handle is used to hold the window handle id of the browser window in focus. To close only the active or current tab we have to use the close method.
Syntax
parent = driver.window_handles[0] chld = driver.window_handles[1] driver.switch_to.window(chld) driver.close()
Let us try to close active browser as shown in the below image −
Example
from selenium import webdriver #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) #launch URL driver.get("https://accounts.google.com/") #identify element m = driver.find_element_by_link_text("Help") m.click() #obtain parent window handle p= driver.window_handles[0] #obtain browser tab window c = driver.window_handles[1] #switch to tab browser driver.switch_to.window(c) print("Page title :") print(driver.title) #close browser tab window driver.close() #switch to parent window driver.switch_to.window(p) print("Current page title:") print(driver.title) #close browser parent window driver.quit()
Output
- Related Articles
- How to close a current tab in a browser window using JavaScript?
- How to close a browser session in Selenium with python?
- How do I switch to the active tab in Selenium?
- How to close the whole browser window by keeping the webDriver active?
- How to close child browser window in Selenium WebDriver using Java?
- Selenium testing without browser.
- How to check whether or not a browser tab is currently active using JavaScript?
- How to open a link in new tab of chrome browser using Selenium WebDriver?
- How to open new tab in same browser and switch between them using Selenium?
- How to invoke the Chrome browser in Selenium with python?
- How to invoke the Firefox browser in Selenium with python?
- How to invoke the IE browser in Selenium with python?
- C Sharp with Selenium - How to Switch one tab to another tab in Csharp Selenium?
- How to maximize the browser in Selenium?
- Python selenium browser driver.back().

Advertisements