5

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

4

2 回答 2

4

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

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

于 2016-06-22T13:00:17.177 回答
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
于 2016-06-22T09:51:55.410 回答