Selenium常见异常:WebDriverException:信息:连接被拒绝。

16

这是我的代码:

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.python.org')

browser.close()
当我运行这个脚本时,它启动了 Firefox 浏览器,但页面是空白的,然后命令行显示错误消息:

运行此脚本时,它启动了 Firefox 浏览器,但页面为空白,并且命令行显示错误消息:

Traceback (most recent call last):
  File "ad.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 76, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

我的Python版本是2.7.3,Selenium版本是selenium-3.0.0.b3.egg-info

请问,如何解决这个问题...


它对我来说运行良好。你是在使用代理吗? - Jeril
不,绝不使用代理。 - leven
你解决了这个问题吗,@leven? - Timir
7个回答

17

检查您的geckodriver.log文件(应与Python文件在同一目录中)

如果它显示Error: GDK_BACKEND does not match available displays,则安装pyvirtualdisplay:

pip install pyvirtualdisplay selenium

你可能也需要 xvfb:

sudo apt-get install xvfb # Debian

sudo yum install Xvfb # Fedora

然后尝试添加这段代码:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()

完整示例:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.python.org')

browser.close()

1
我可以确认,像这样使用虚拟显示器已经解决了我的问题。 - exhuma
1
对我来说有效,但需要在Centos上使用Xvfp(大写X)。 - JMG

4
正如@kervvv所提到的,这个问题很可能与Firefox的旧版本有关,而不是selenium和/或geckodriver期望或需要的版本。值得注意的是,据我所知,来自selenium的具体错误消息有些通用或模糊,因此它并没有明确显示错误的原因。
如果用户在使用Firefox的旧版本(包括扩展支持版本ESR)时遇到问题,请尝试以下解决方案。
  1. Visit the Firefox download page to download a Beta, Nightly, or Developer version of Firefox.
  2. Extract the package into an arbitrary location on your filesystem (anywhere you want)
  3. Specify the FirefoxBinary within your code or script to point to the downloaded location.

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    binary = FirefoxBinary('/home/username/firefox/firefox')
    driver = webdriver.Firefox(firefox_binary=binary)
    driver.get(url)
    
在Gentoo上,例如,在Firefox(ESR)版本为52的同时,geckodriver(0.20.0)和selenium(3.11.0)的版本是最新的可用上游版本。

3

我曾经遇到同样的问题,认为它与代理或端口有关(但没有用),但解决我的问题的方法只是简单地更新Firefox。我运行的版本是52.0.xxx,然后更新到了57.0.2。链接在这里


3

我也遇到了这个问题,需要设置DISPLAY。对于我来说,Xvfb帧缓冲正在本地机器上的:99运行。

$ export DISPLAY=:99

2
这可能是由于各种原因造成的。
  • Most probably because the "latest" version of your geckodriver is not able to communicate with your "slightly older" firefox.

  • The easiest way to fix this would be to try out different older versions of geckodriver. Run the following command to find the current version of your geckodriver

    geckodriver --version
    
  • If it shows the version as 19 or above, execute following steps to use geckodriver version 17(Works 90% of times)

    1. Your existing geckodriver most typically may be placed in /usr/local/bin when u installed it earlier. First delete this by running sudo rm -r /usr/local/bin/geckodriver

    2. Download version 17 of geckodriver from this link. Move the downloaded file(geckodriver-v0.17.0-arm7hf.tar.gz) from your Downloads folder into your home directory

    3. Unpack the file

      tar -xzvf geckodriver-v0.17.0-arm7hf.tar.gz
      

      This will create a folder called "geckodriver" in your home directory

    4. Move/Copy this extracted "geckodriver" into /usr/local/bin/

      sudo cp geckodriver /usr/local/bin/
      
    5. Run

      sudo reboot
      

现在重新运行你的程序...
它应该可以工作了!


2

0

1
0.29.1 有相同的问题。 - Mario Palumbo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接