Skip to content

Instantly share code, notes, and snippets.

@amagee
Last active December 15, 2016 01:14
Show Gist options
  • Save amagee/d49037153e0b87eaf295cbdaefbb77ef to your computer and use it in GitHub Desktop.
Save amagee/d49037153e0b87eaf295cbdaefbb77ef to your computer and use it in GitHub Desktop.
Selenium switch tabs
# Current versions of Selenium are so silly that if an action on the page
# results in a new tab being opened, the browser will switch to the new tab,
# but the selenium driver will still think the old tab is visible, so if you
# try to access elements on the old tab, they will fail with 'element not visible'
# errors.
# One way to fix this is to explicitly switch to the new tab and close it
# (if you have an element that will close the tab).
driver.switch_to.window(driver.window_handles[1])
driver.find_element_by_css_selector(".my-close-button").click()
driver.switch_to.window(driver.window_handles[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment