selenium.common.exceptions.SessionNotCreatedException:消息:无法找到与 GeckoDriver、Selenium 和 Firefox 匹配的一组功能 [英] selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities with GeckoDriver, Selenium and Firefox

查看:28
本文介绍了selenium.common.exceptions.SessionNotCreatedException:消息:无法找到与 GeckoDriver、Selenium 和 Firefox 匹配的一组功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed a python script with selenium with firefox webdriver. It is workign fine in my machine. but if i execute the same script in another machine it is giving the following error.

Traceback (most recent call last):
  File "insurance_web_monitor.py", line 13, in <module>
    driver = Firefox(executable_path='geckodriver', firefox_options=options)
  File "C:Python34libsite-packagesseleniumwebdriverfirefoxwebdriver.py",
line 167, in __init__
    keep_alive=True)
  File "C:Python34libsite-packagesseleniumwebdriver
emotewebdriver.py", l
ine 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:Python34libsite-packagesseleniumwebdriver
emotewebdriver.py", l
ine 251, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:Python34libsite-packagesseleniumwebdriver
emotewebdriver.py", l
ine 320, in execute
    self.error_handler.check_response(response)
  File "C:Python34libsite-packagesseleniumwebdriver
emoteerrorhandler.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

在其他机器上的配置是,

The configurations in other machine are,

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

是的,每个配置都完全相同.尽管这看起来很愚蠢,但它不起作用并且折磨着我.我错过了机器之间要考虑的任何事情吗?提前致谢.

Yes every configuration is exactly same. Though this seems silly it is not working and it's torturing me. Am i missing anything to consider between the machines? Thanks in advance.

推荐答案

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

As per your question and code block as you are using the following Test Configuration:

  • 硒 => 3.14
  • geckodriver => 0.21.0
  • Firefox => 61.0.2

您必须强制使用牵线木偶功能.要实现这一点:

You have to use the capability marionette mandatorily. To achieve that either:

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

  • You can leave the capability marionette untouched as by default marionette is set to True.
  • You can also specify the capability marionette as follows:

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

这个错误信息...

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

...表示 GeckoDriver 无法启动/生成新的 WebBrowser,即 Firefox 浏览器 会话.

...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.

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

There are numerous possibilities behind the error you are seeing and can be solved through any of the following steps mentioned below:

  • 当您使用 Windows 操作系统时,您需要将 key executable_pathvalue 一起传递> 包含:

  • As you are on Windows OS you need to pass the key executable_path along with the value containing:

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

  • Absolute path of the GeckoDriver.
  • The Absolute path of the GeckoDriver should be mentioned through single quotes and single backward slash along with the raw (r) switch.
  • Include the extension of the GeckoDriver binary.
  • Your line of code will be:

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

这篇关于selenium.common.exceptions.SessionNotCreatedException:消息:无法找到与 GeckoDriver、Selenium 和 Firefox 匹配的一组功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆