【问题标题】:selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities with GeckoDriver, Selenium and Firefoxselenium.common.exceptions.SessionNotCreatedException:消息:无法找到与 GeckoDriver、Selenium 和 Firefox 匹配的一组功能
【发布时间】:2019-01-30 20:18:14
【问题描述】:

我已经开发了一个带有 selenium 和 firefox webdriver 的 python 脚本。在我的机器上工作正常。但如果我在另一台机器上执行相同的脚本,则会出现以下错误。

Traceback (most recent call last):
  File "insurance_web_monitor.py", line 13, in <module>
    driver = Firefox(executable_path='geckodriver', firefox_options=options)
  File "C:\Python34\lib\site-packages\selenium\webdriver\firefox\webdriver.py",
line 167, in __init__
    keep_alive=True)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 251, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 320, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a
 matching set of capabilities

这是我的代码

import os
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


if __name__ == "__main__":
    options = Options()
    cap = DesiredCapabilities().FIREFOX
    cap["marionette"] = False
    options.add_argument('-headless')
    driver = Firefox(executable_path='geckodriver', firefox_options=options, capabilities=cap)
    wait = WebDriverWait(driver, timeout=10)
    driver.get('http://www.google.com')
    driver.save_screenshot(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), os.getcwd(), 'screenshot1.png'))
    wait.until(expected.visibility_of_element_located((By.NAME, 'q'))).send_keys('headless firefox' + Keys.ENTER)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, '#ires a'))).click()
    print(driver.page_source)
    driver.quit()

我机器上的配置是,

Windows => 7 Professional 64-bit
Python => 3.4.4
Selenium => 3.14
Firefox => 61.0.2
geckodriver => 0.21.0

其他机器上的配置是,

Windows => 7 Professional 64-bit
Python => 3.4.4
Selenium => 3.14
Firefox => 61.0.2
geckodriver => 0.21.0

是的,每个配置都完全相同。虽然这看起来很愚蠢,但它不起作用,它在折磨我。 我在机器之间有什么需要考虑的吗? 提前致谢。

【问题讨论】:

  • 你试过caps['marionette'] = False
  • @SmashGuy 使用错误跟踪日志更新问题。
  • @NarendraR,是的。但这会导致selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Possible firefox version mismatch 错误。修改有问题的代码
  • @DebanjanB 添加了错误日志
  • 当您使用Firefox =&gt; 61.0.2 时,您不能使用配置cap["marionette"] = False。保持marionette的配置为默认true默认)。

标签: python selenium firefox selenium-webdriver geckodriver


【解决方案1】:

根据您的问题和代码块,您正在使用以下测试配置

  • 硒 => 3.14
  • geckodriver => 0.21.0
  • 火狐=> 61.0.2

你必须强制使用ma​​rionette能力。要实现这一点:

  • 您可以保持 ma​​rionette 功能不变,因为默认情况下 marionette 设置为 True
  • 您还可以指定能力ma​​rionette,如下所示:

    cap = DesiredCapabilities().FIREFOX
    cap["marionette"] = True
    

这个用例

此错误消息...

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

...暗示 GeckoDriver 无法启动/生成新的 WebBrowserFirefox Browser 会话。

您看到的错误背后有多种可能性,可以通过下面提到的以下任何步骤来解决:

  • 当您在 Windows 操作系统 上时,您需要传递 key executable_path 以及包含以下内容的 value

    • GeckoDriver绝对路径
    • GeckoDriver绝对路径 应通过单引号和单反斜杠以及原始 (r) 开关提及。
    • 包括 GeckoDriver 二进制文件的扩展。
    • 您的代码行将是:

      driver = Firefox(executable_path=r'C:\path\to\geckodriver.exe', firefox_options=options, capabilities=cap)
      

参考

【讨论】:

  • +1 您的时间和解释。谢谢。但这也行不通。我无法解决错误。
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 2018-07-30
  • 2020-04-28
  • 1970-01-01
  • 2020-07-30
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多