我可以重新连接到硒会话,但不能阻止它启动其他浏览器吗? [英] I can reattach to session with selenium but cannot stop it launching another browser?
问题描述
这是我的代码,它确实附加到旧会话,但是在调用webdriver.Remote()时,它将启动另一个浏览器!没原因?这是在Mac上,其他人还有这个问题吗? (这使该功能无用)
this is my code, it DOES attach to the old session, however on calling webdriver.Remote() it makes another browser launch!! for no reason? This is on Mac, does anyone else have this issue? (it makes this feature useless)
有人可以告诉我我在做什么错吗?
can anyone tell me what i am doing wrong?
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")
推荐答案
您可以执行此操作,但需要对硒代码进行一些修补.这可以通过在python中使用猴子补丁来完成.下面是相同的代码
You can do that but it needs some patching to selenium code. This can be done using monkey patching in python. Below is the code for the same
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.我们在下面的部分中做
The key of the solution is not to let newSession
command get executed through the original driver, rather handle it send our own existing session id. Which we do in the below part
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/
PS: Detailed article available on http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/
这篇关于我可以重新连接到硒会话,但不能阻止它启动其他浏览器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!