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

时间:2016-06-22 09:23:07

标签: python session selenium firefox geckodriver

升级到geckodriver后,我无法重复使用我的Selenium会话。这是我的设置:

我有一个buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "net.ltgt.gradle:gradle-apt-plugin:0.6" } } apply plugin: 'java' apply plugin: "net.ltgt.apt" apply plugin: "idea" dependencies { // Dagger 2 and Compiler compile 'com.google.dagger:dagger:2.0.2' apt "com.google.dagger:dagger-compiler:2.0.2" } 脚本,它会启动一个Firefox实例并打印一个要连接的端口,例如:

start_browser.py

...和另一个脚本,它试图通过远程驱动程序连接到现有实例:

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一样?或者这是geckodriver的预期行为(希望不是)?

2 个答案:

答案 0 :(得分:3)

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

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等门票。

答案 1 :(得分:0)

您可以使用会话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