selenium閲嶇敤浼氳瘽
chrome
from selenium import webdriver
driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://tarunlalwani.com")
print session_id
print executor_url
driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
driver2.session_id = session_id
print driver2.current_url
firefox
from selenium import webdriver
driver = webdriver.Firefox()
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://tarunlalwani.com")
print session_id
print executor_url
def create_driver_session(session_id, executor_url):
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
# Save the original function, so we can revert our patch
org_command_execute = RemoteWebDriver.execute
def new_command_execute(self, command, params=None):
if command == "newSession":
# Mock the response
return {'success': 0, 'value': None, 'sessionId': session_id}
else:
return org_command_execute(self, command, params)
# Patch the function before creating the driver object
RemoteWebDriver.execute = new_command_execute
new_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
new_driver.session_id = session_id
# Replace the patched function with original function
RemoteWebDriver.execute = org_command_execute
return new_driver
driver2 = create_driver_session(session_id, executor_url)
print driver2.current_url
http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/
selenium閲嶇敤浼氳瘽锛屽苟浣跨敤涔嬪墠鐨刢hrome瀹炰緥
Allow webdriver to attach to a running browser #18 https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/18
You may be interested in what I have implemented as PersistentWebdriver class that allows to attach to a running session:
鍩轰簬remote connection瀹炵幇锛岀洿鎺ユ搷绾礳hromedriver(HTTP鎺ュ彛) https://gist.github.com/852560
鍩轰簬selenium鏀硅繘鐗堟湰鐨剋ebdriver.py
I got a solution in python, I modified the webdriver class bassed on PersistenBrowser class that I found.
https://github.com/axelPalmerin/personal/commit/fabddb38a39f378aa113b0cb8d33391d5f91dca5
replace the webdriver module /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py
Ej. to use:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
runDriver = sys.argv[1]
sessionId = sys.argv[2]
def setBrowser():
if eval(runDriver):
webdriver = w.Remote(command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)
else:
webdriver = w.Remote(command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
session_id=sessionId)
url = webdriver.command_executor._url
session_id = webdriver.session_id
print url
print session_id
return webdriver
This snippet successfully allows to reuse existing browser instance yet avoiding raising the duplicate browser. Found at Tarun Lalwani’s blog.
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
# executor_url = driver.command_executor._url
# session_id = driver.session_id
def attach_to_session(executor_url, session_id):
original_execute = WebDriver.execute
def new_command_execute(self, command, params=None):
if command == "newSession":
# Mock the response
return {'success': 0, 'value': None, 'sessionId': session_id}
else:
return original_execute(self, command, params)
# Patch the function before creating the driver object
WebDriver.execute = new_command_execute
driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
driver.session_id = session_id
# Replace the patched function with original function
WebDriver.execute = original_execute
return driver
bro = attach_to_session('http://127.0.0.1:64092', '8de24f3bfbec01ba0d82a7946df1d1c3')
bro.get('http://ya.ru/')
https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session/34394556
https://github.com/axelPalmerin/personal/commit/fabddb38a39f378aa113b0cb8d33391d5f91dca5
https://github.com/axelPalmerin/personal/blob/fabddb38a39f378aa113b0cb8d33391d5f91dca5/webdriver.py
https://raw.githubusercontent.com/axelPalmerin/personal/fabddb38a39f378aa113b0cb8d33391d5f91dca5/webdriver.py
wget https://raw.githubusercontent.com/axelPalmerin/personal/fabddb38a39f378aa113b0cb8d33391d5f91dca5/webdriver.py
sed -i 's#session_id=None#options=None,session_id=None#g' webdriver.py
selenium鍘熺敓webdriver.py https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/remote/webdriver.py