MelihTozlu
MelihTozlu

Reputation: 11

Close the new opened tab - Python (Selenium)

I am trying to close a new tab which I have opened with

driver.execute_script("window.open('https://www.google.com/');")

However, it is not working with the ways that I've tried. Which are:

ActionChains(driver).key_down(Keys.SHIFT).send_keys('w').key_up(Keys.SHIFT).perform()

And this one does work partially (it closes the driver tab which is not the goal):

driver.close

The goal is to close the new freshly opened window/tab in Chrome (Windows).

Upvotes: 0

Views: 59

Answers (1)

Prophet
Prophet

Reputation: 33381

To close the newly opened tab you first have to switch to that tab with

driver.switch_to.window(driver.window_handles[1])

now you should close it with the

driver.close()

and then switch back to the main tab with

driver.switch_to.window(driver.window_handles[0])

Upvotes: 1

Related Questions