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

时间:2018-04-17 19:05:07

标签: python selenium firefox 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

堆栈跟踪

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

2 个答案:

答案 0 :(得分:2)

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

  • 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 清除您的项目工作区仅使用所需的依赖关系重建您的项目。

  • 使用CCleaner工具清除执行 Test Suite 之前和之后的所有操作系统杂务。
  • 如果您的基本 Web客户端版本太旧,请通过Revo Uninstaller将其卸载并安装最新的GA和已发布的 Web客户端版本。
  • 进行系统重启
  • 执行@Test
  • 始终在driver.quit()方法中调用tearDown(){}以关闭&正常销毁 WebDriver Web客户端实例。

替代

作为替代方案,您还可以尝试使用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/')

您可以在此处找到有关How to make firefox headless programatically in Selenium with python?

的详细讨论

答案 1 :(得分:1)

这两个命令都在同一个端口上启动webdriver。第二个导致错误,因为端口已在使用中:

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

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

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