Python selenium.common.exceptions.WebDriverException:消息:连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39547598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selenium.common.exceptions.WebDriverException: Message: connection refused
提问by leven
Here is my code:
这是我的代码:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')
browser.close()
It launched the firefox browser when I ran this script, but the page is blank, then the command line shows the error message:
当我运行这个脚本时,它启动了 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
My python vesion is 2.7.3and the selenium version is selenium-3.0.0.b3.egg-info
我的 python 版本是2.7.3,硒版本是selenium-3.0.0.b3.egg-info
Please, how do I solve the problem ...
请问怎么解决这个问题...
回答by Cortexelus
Check your geckodriver.log
file (should be in the same directory as python file)
检查您的geckodriver.log
文件(应该与python文件在同一目录中)
If it says Error: GDK_BACKEND does not match available displays
then install pyvirtualdisplay:
如果它说Error: GDK_BACKEND does not match available displays
然后安装pyvirtualdisplay:
pip install pyvirtualdisplay selenium
You might need xvfb too:
您可能也需要 xvfb:
sudo apt-get install xvfb # Debian
sudo yum install Xvfb # Fedora
Then try adding this code:
然后尝试添加以下代码:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
Full example:
完整示例:
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()
回答by ILMostro_7
As mentioned by @kervvv this issue is probably related to an older version of Firefox than what the version of selenium
and/or geckodriver
expect(s) or need(s). It should be noted, as far as I can tell, that the specific error message from selenium is somewhat generic or vague; so, it doesn't explicitly show why the error occurs.
正如@kervvv 所提到的,这个问题可能与旧版本的 Firefox 相关,而不是版本selenium
和/或geckodriver
期望或需要的版本。应该注意的是,据我所知,来自 selenium 的特定错误消息有些笼统或含糊;所以,它没有明确显示错误发生的原因。
In case users are here looking for help while using an older version of Firefox, including the Extended Support Release (ESR), the following solution should work just fine.
如果用户在使用旧版 Firefox 时寻求帮助,包括扩展支持版本 (ESR),以下解决方案应该可以正常工作。
- Visit the Firefox download pageto download a Beta, Nightly, or Developer version of Firefox.
- Extract the package into an arbitrary location on your filesystem (anywhere you want)
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)
- 访问Firefox 下载页面以下载Firefox的 Beta、Nightly 或 Developer 版本。
- 将包解压缩到文件系统上的任意位置(您想要的任何位置)
指定
FirefoxBinary
代码或脚本,以点内下载的位置。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)
That works for me on Gentoo, for example, where the version of geckodriver
(0.20.0) and selenium
(3.11.0) are the latest available upstream, while Firefox (ESR) is at version 52.
例如,这在 Gentoo 上对我geckodriver
有用,其中(0.20.0) 和selenium
(3.11.0) 的版本是最新的上游可用版本,而 Firefox (ESR) 的版本为 52。
回答by shoka
Had this problem as well. Needed to set the DISPLAY. For me Xvfb frame buffer is running on local machine at :99 so.
也有这个问题。需要设置 DISPLAY。对我来说,Xvfb 帧缓冲区在本地机器上运行:99 所以。
$ export DISPLAY=:99
回答by Kervvv
回答by sharat kanthi
This could be for various reasons.
这可能是出于各种原因。
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)
Your existing geckodriver most typically may be placed in
/usr/local/bin
when u installed it earlier. First delete this by runningsudo rm -r /usr/local/bin/geckodriver
Download version 17 of geckodriver from this link. Move the downloaded file(
geckodriver-v0.17.0-arm7hf.tar.gz
) from yourDownloads
folder into yourhome
directoryUnpack the file
tar -xzvf geckodriver-v0.17.0-arm7hf.tar.gz
This will create a folder called "geckodriver" in your home directory
Move/Copy this extracted "geckodriver" into
/usr/local/bin/
sudo cp geckodriver /usr/local/bin/
Run
sudo reboot
很可能是因为您的 geckodriver 的“最新”版本无法与您的“稍旧”的 firefox 通信。
解决此问题的最简单方法是尝试不同的旧版本 geckodriver。运行以下命令以查找 geckodriver 的当前版本
geckodriver --version
如果显示版本为 19 或以上,请执行以下步骤以使用 geckodriver 版本 17(工作 90% 次)
您现有的 geckodriver 通常可能会在
/usr/local/bin
您之前安装时放置在其中。首先通过运行删除它sudo rm -r /usr/local/bin/geckodriver
从此链接下载 geckodriver 的第 17 版。将下载的文件 (
geckodriver-v0.17.0-arm7hf.tar.gz
) 从您的Downloads
文件夹移动到您的home
目录中解压文件
tar -xzvf geckodriver-v0.17.0-arm7hf.tar.gz
这将在您的主目录中创建一个名为“geckodriver”的文件夹
将此提取的“geckodriver”移动/复制到
/usr/local/bin/
sudo cp geckodriver /usr/local/bin/
跑
sudo reboot
Rerun your program now...
It should work!
现在重新运行你的程序......
它应该可以工作!
回答by Peter
First thing to do:Update Firefox and make sure you have the latest version of geckodriver installed (https://github.com/mozilla/geckodriver/releases)
要做的第一件事:更新 Firefox 并确保您安装了最新版本的 geckodriver ( https://github.com/mozilla/geckodriver/releases)
回答by Sri
I got the same error. After updating the geckodriver vresion to geckodriver 0.24.0 ( 2019-01-28) worked fine for me.
我得到了同样的错误。将 geckodriver vresion 更新为 geckodriver 0.24.0 ( 2019-01-28) 后,对我来说效果很好。
Download source :https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz
下载源:https: //github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz