Python 无法通过geckodriver连接到现有的Selenium会话
升级到geckodriver后,我无法重用Selenium的会话。以下是我的设置: 我有一个Python 无法通过geckodriver连接到现有的Selenium会话,python,session,selenium,firefox,geckodriver,Python,Session,Selenium,Firefox,Geckodriver,升级到geckodriver后,我无法重用Selenium的会话。以下是我的设置: 我有一个start\u browser.py脚本,它启动一个Firefox实例并打印一个要连接的端口,比如: firefox_capabilities = DesiredCapabilities.FIREFOX firefox_capabilities['marionette'] = True browser = webdriver.Firefox(capabilities=firefox_capabilitie
start\u 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次播放效果很好),并关注类似这样的罚单。仍然不走运。它在我分配会话之前就崩溃了,在
webdriver.Remote()构造函数中。当前实现似乎具有硬编码的新会话创建:。有什么想法吗?@AlexMorozov这不应该是问题,因为这对我来说很有效。唯一的区别是我没有使用木偶。您是否使用webdriver.Remote
调用{}
的功能
进行了尝试,还是添加了新的功能
?尝试了两种功能
选项。事实上,它也一直在为我工作,直到最近版本的Firefox不推荐旧的驱动程序,并使木偶成为与浏览器交互的唯一方式。所以问题是,考虑到新的条件,如何重新连接到会话。如何获取手动启动的浏览器的会话id?现在呢?