Selenium Python selenium.common.exceptions.WebDriverException:消息:使用 geckodriver 和 firefox 拒绝连接

Selenium Python selenium.common.exceptions.WebDriverException: Message: connection refused using geckodriver and firefox

我在 运行我的自动化测试脚本中遇到了问题。当我 运行 我的脚本时,将出现一个浏览器,但它不会键入 URL 并等待 10 秒,直到它抛出异常。有没有我可以使用的解决方案,以便我可以让我的自动化测试脚本工作?

Geckodriver.log:

1523997052492   geckodriver INFO    geckodriver 0.20.1
1523997052531   geckodriver INFO    Listening on 127.0.0.1:37807
1523997052592   mozrunner::runner   INFO    Running command: "/usr/bin/firefox/firefox" "-marionette" "--headless" "-profile" "/tmp/rust_mozprofile.PU1cngaAJ5Tg"
1523997054831   Marionette  INFO    Listening on port 2828

堆栈跟踪:

Error
Traceback (most recent call last):
File 
"/home/kavin/PycharmProjects/untitled/Testing/purchaseAmazonItems.py", line 13, in setUp
self.driver = webdriver.Firefox(firefox_binary=binary, firefox_options=opts)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

代码:

def setUp(self):
    binary = FirefoxBinary('/usr/bin/firefox/firefox')
    opts = FirefoxOptions()
    opts.add_argument("--headless")
    self.driver = webdriver.Firefox(firefox_binary=binary, firefox_options=opts)
    driver = self.driver
    driver.get('https://www.amazon.com/')

规格

Ubuntu 16.04
geckodriver 0.20.1
firefox 59.0.2+build1-0ubuntu0.16.04.3
Python 3.6
Pycharm 2016.3
Selenium 3.11.0

这两个命令都在同一端口上启动网络驱动程序。第二个导致错误,因为端口已被使用:

self.driver = webdriver.Firefox(firefox_binary=binary)
browser = webdriver.Firefox(firefox_options=opts)

要更正此问题,请在初始化驱动程序之前设置选项(在第一个命令中)。

self.driver = webdriver.Firefox(firefox_binary=binary, firefox_options=opts)

没有错误堆栈跟踪 配置问题很难调试。话虽如此,我在您的代码块中没有发现任何重大问题。您可能需要执行一些额外的步骤,如下所示:

  • 传递 Key executable_path 连同 Value引用GeckoDriver的绝对路径如下:

    def setUp(self):
        binary = FirefoxBinary('/usr/bin/firefox/firefox')
        opts = FirefoxOptions()
        opts.add_argument("--headless")
        self.driver = webdriver.Firefox(firefox_binary=binary, firefox_options=opts, executable_path='/path/to/geckodriver')
        driver = self.driver
        driver.get('https://www.amazon.com/')
    
  • 通过IDE[=54=清理您的项目工作区 ] 和 重建 您的项目仅具有必需的依赖项。

  • 使用 CCleaner 工具清除执行 测试套件 .
  • 前后的所有 OS 琐事
  • 如果您的基础 Web 客户端 版本太旧,则通过 Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web 客户端.
  • 系统重启
  • 执行你的@Test.
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例.

备选

作为替代方案,您也可以尝试使用 set_headless(headless=boolean_value),如下所示:

def setUp(self):
    binary = FirefoxBinary('/usr/bin/firefox/firefox')
    opts = FirefoxOptions()
    opts.set_headless(headless=True)
    self.driver = webdriver.Firefox(firefox_binary=binary, firefox_options=opts, executable_path='/path/to/geckodriver')
    driver = self.driver
    driver.get('https://www.amazon.com/')

在这里你可以找到关于

的详细讨论