Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active May 16, 2019 20:37
Show Gist options
  • Save sauceaaron/6e463d9e4c2ed418c1a03a1b95affa26 to your computer and use it in GitHub Desktop.
Save sauceaaron/6e463d9e4c2ed418c1a03a1b95affa26 to your computer and use it in GitHub Desktop.
Hijack an existing webdriver session and keep it running in the background while you debug
#!/usr/bin/env python
# ghost_session.py
# usage: python ghost_session.py <running_session_id>
from os import environ
from sys import argv
from time import sleep
from selenium import webdriver
# get the session id you want to take over from command line argument
running_session_id = argv[1]
print("session:" + running_session_id)
# get sauce labs credentials from environment variables
username = environ["SAUCE_USERNAME"]
accesskey = environ["SAUCE_ACCESS_KEY"]
# default command executor URL and capabilities for a remote session
url = "http://" + username + ":" + accesskey + "@" + "ondemand.saucelabs.com/wd/hub"
caps = { "browserName": "Chrome", "platform": "Windows 10", "name": "ghost session" }
# create an initial session that will be discarded
driver = webdriver.Remote(url, caps)
old_session_id = driver.session_id
# this will now send requests to the other session
driver.session_id = running_session_id
# keep session alive with non-intrusive webdriver requests
while True:
print(driver.title)
sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment