微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 无法通过geckodriver附加到现有的Selenium会话

升级到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的预期行为(希望不是)?

解决方法:

好吧,所以除非有人提出更优雅的解决方案,否则这是一个快速的肮脏黑客:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐