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

kav*_*kav 5 python firefox selenium selenium-webdriver geckodriver

我在运行自动化测试脚本时遇到问题。当我运行我的脚本时,会出现一个浏览器,但它不会输入 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
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

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
Run Code Online (Sandbox Code Playgroud)

代码:

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/')
Run Code Online (Sandbox Code Playgroud)

规格:

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

Deb*_*anB 4

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

  • 传递Keyexecutable_path 以及引用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/')
    
    Run Code Online (Sandbox Code Playgroud)
  • 通过IDE清理项目工作区并仅使用所需的依赖项重建项目。

  • 使用CCleaner工具清除测试套件执行前后的所有操作系统杂务。
  • 如果您的基本Web 客户端版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和发布版本的Web 客户端。
  • 重新启动系统。
  • 执行你的@Test.
  • driver.quit()始终在方法内调用tearDown(){}以优雅地关闭和销毁WebDriver和Web 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/')
Run Code Online (Sandbox Code Playgroud)

在这里你可以找到关于How to make firefox headlessprogramatically in Selenium with python? 的详细讨论。