Skip to content

Instantly share code, notes, and snippets.

@uluQulu
Last active September 1, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uluQulu/83a4eb51a84e08bcf9013c2cce5431f1 to your computer and use it in GitHub Desktop.
Save uluQulu/83a4eb51a84e08bcf9013c2cce5431f1 to your computer and use it in GitHub Desktop.
Open a new tab; Do stuff in it; Close it
@contextmanager
def new_tab(browser):
""" USE once a host tab must remain untouched and yet needs extra data- get from guest tab """
try:
# add a guest tab
browser.execute_script("window.open()")
# switch to the guest tab
browser.switch_to.window(browser.window_handles[1])
yield
finally:
# close the guest tab
browser.execute_script("window.close()")
# return to the host tab
browser.switch_to.window(browser.window_handles[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment