无法通过 geckodriver 附加到现有的 Selenium 会话
Cannot attach to an existing Selenium session via geckodriver
升级到 geckodriver 后,我无法重用我的 Selenium 会话。这是我的设置:
我有一个 start_browser.py
脚本,它启动一个 Firefox 实例并打印一个要连接的端口,例如:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities)
print browser.service.port
wait_forever()
...和另一个脚本,它试图通过远程驱动程序连接到现有实例:
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
driver = webdriver.Remote(
command_executor='http://localhost:{port}'.format(port=port),
desired_capabilities=caps)
但它似乎正在尝试启动一个新会话,但失败并显示一条消息:
selenium.common.exceptions.WebDriverException: Message: Session is already started
是否可以像以前版本的 Selenium 那样仅附加到现有会话?或者这是 geckodriver 的预期行为(希望不是)?
您可以使用会话 ID 重新连接到会话。
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities)
print browser.service.port
wait_forever()
# get the ID and URL from the browser
url = browser.command_executor._url
session_id = browser.session_id
# Connect to the existing instance
driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.session_id = session_id
好吧,除非有人想出更优雅的解决方案,否则这里有一个快速肮脏的技巧:
class SessionRemote(webdriver.Remote):
def start_session(self, desired_capabilities, browser_profile=None):
# Skip the NEW_SESSION command issued by the original driver
# and set only some required attributes
self.w3c = True
driver = SessionRemote(command_executor=url, desired_capabilities=caps)
driver.session_id = session_id
糟糕的是,它仍然无法正常工作,抱怨它不知道 moveto
命令,但至少它连接到已启动的浏览器。
更新: geckodriver 目前似乎缺少一些功能,所以如果你们要继续使用 Firefox,只需将其降级到支持旧 webdriver 的版本即可(45 玩得很好),并留意像 https://github.com/SeleniumHQ/selenium/issues/2285 .
这样的门票
升级到 geckodriver 后,我无法重用我的 Selenium 会话。这是我的设置:
我有一个 start_browser.py
脚本,它启动一个 Firefox 实例并打印一个要连接的端口,例如:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities)
print browser.service.port
wait_forever()
...和另一个脚本,它试图通过远程驱动程序连接到现有实例:
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
driver = webdriver.Remote(
command_executor='http://localhost:{port}'.format(port=port),
desired_capabilities=caps)
但它似乎正在尝试启动一个新会话,但失败并显示一条消息:
selenium.common.exceptions.WebDriverException: Message: Session is already started
是否可以像以前版本的 Selenium 那样仅附加到现有会话?或者这是 geckodriver 的预期行为(希望不是)?
您可以使用会话 ID 重新连接到会话。
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities)
print browser.service.port
wait_forever()
# get the ID and URL from the browser
url = browser.command_executor._url
session_id = browser.session_id
# Connect to the existing instance
driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.session_id = session_id
好吧,除非有人想出更优雅的解决方案,否则这里有一个快速肮脏的技巧:
class SessionRemote(webdriver.Remote):
def start_session(self, desired_capabilities, browser_profile=None):
# Skip the NEW_SESSION command issued by the original driver
# and set only some required attributes
self.w3c = True
driver = SessionRemote(command_executor=url, desired_capabilities=caps)
driver.session_id = session_id
糟糕的是,它仍然无法正常工作,抱怨它不知道 moveto
命令,但至少它连接到已启动的浏览器。
更新: geckodriver 目前似乎缺少一些功能,所以如果你们要继续使用 Firefox,只需将其降级到支持旧 webdriver 的版本即可(45 玩得很好),并留意像 https://github.com/SeleniumHQ/selenium/issues/2285 .
这样的门票