python - 无法通过 geckodriver 附加到现有的 Selenium session

标签 python session selenium firefox geckodriver

升级到 geckodriver 后,我无法重用我的 Selenium session 。这是我的设置:

我有一个 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)

但它似乎正在尝试启动一个新 session ,但失败并显示一条消息:

selenium.common.exceptions.WebDriverException: Message: Session is already started

是否可以像在以前版本的 Selenium 中那样仅附加到现有 session ?或者这是 geckodriver 的预期行为(希望不是)?

最佳答案

好吧,除非有人想出更优雅的解决方案,否则这里有一个快速的肮脏 hack:

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 这样的门票.

关于python - 无法通过 geckodriver 附加到现有的 Selenium session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963785/

相关文章:

c# - 计算 Web 应用程序的在线用户数

django - 如何在 django 项目中使用 Selenium(LiveServerTestCase) 并行运行测试?

python - 找出 4 个数字,其总和为给定的数字

python - 在 python 中滚动 idxmax() ?

java - 如果用户未登录struts2,如何防止加载任何页面

java - Selenium 无法在我的 html 页面中找到链接

c# - 在 Selenium Webdriver 中读取分页数据网格

python - Three.js - 使用 python 导出 obj 文件

python - Python 操作顺序

hibernate - 回滚是否终止 session ?