这是我的代码,它附加到旧会话,但是在调用webdriver.Remote()时它会启动另一个浏览器!没原因?这是在Mac上,有没有其他人有这个问题? (它使这个功能无用)
谁能告诉我我做错了什么?from selenium import webdriver
driver = webdriver.Chrome()
url = driver.command_executor._url
session_id = driver.session_id
driver.get('https://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro')
driver2 = webdriver.Remote(command_executor=url,desired_capabilities={})
driver2.session_id = session_id
driver2.get("http://www.wikipedia.in")
答案 0 :(得分:1)
你可以这样做,但它需要修补selenium代码。这可以使用python中的猴子修补来完成。以下是相同
的代码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
解决方案的关键是不要让newSession
命令通过原始驱动程序执行,而是处理它发送我们自己的现有会话ID。我们在下面的部分中做了什么
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)
PS:详细文章可在http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/
上找到