How to close active/current tab without closing the browser in Selenium-Python?

0 votes
How to close active/current tab without closing the browser in Selenium-Python?
Jul 23 in Selenium by Kabir
2,451 views

1 answer to this question.

0 votes

Hello Kabir, if you want to close only active tab and need to keep the browser window open, you can make use of switch_to.window method which has the input parameter as window handle-id. Following example shows how to achieve this automation:

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('https://www.google.com')

driver.execute_script("window.open('');")
time.sleep(5)

driver.switch_to.window(driver.window_handles[1])
driver.get("https://facebook.com")
time.sleep(5)

driver.close()
time.sleep(5)

driver.switch_to.window(driver.window_handles[0])
driver.get("https://www.yahoo.com")
time.sleep(5)

#driver.close()
answered Jul 23 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
5 answers
0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 13,960 points
272 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
1,128 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,150 points
1,239 views
0 votes
1 answer

How to open new tab in same browser and switch between them using Selenium?

Hi Mugdha, you can use following code ...READ MORE

answered May 24 in Selenium by Abha
• 27,180 points
1,400 views