WebDriverException: 消息: '无法连接到ChromeDriver'。错误在utils.is_connectable(self.port):
我正在尝试使用chromedriver 2.10在CentOS机器上运行我的测试,浏览器版本是Chrome 35.0.1916.114
/home/varunm/EC_WTF_0.4.10/EC_WTF0.4.10_Project/wtframework/wtf/drivers/chromedriver
其实我已经解决了路径的问题,因为如果是路径出错,错误信息会有所不同
def start(self):
"""
Starts the ChromeDriver Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
env = self.env or os.environ
try:
self.process = subprocess.Popen([
self.path,
"--port=%d" % self.port] +
self.service_args, env=env, stdout=PIPE, stderr=PIPE)
except:
raise WebDriverException(
"ChromeDriver executable needs to be available in the path. \
Please download from http://chromedriver.storage.googleapis.com/index.html\
and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to the ChromeDriver")
如果路径不对,我会收到其他的错误信息,但现在的问题是在建立连接时出现错误
5 个回答
这通常意味着你没有使用最新的 ChromeDriver
。要解决这个问题,你可以去这个网站查看最新版本:https://sites.google.com/a/chromium.org/chromedriver/。
Confirm that your chrome version matches.
If you are using Chrome version 73, please download ChromeDriver 73.0.3683.20
If you are using Chrome version 72, please download ChromeDriver 2.46 or ChromeDriver 72.0.3626.69
If you are using Chrome version 71, please download ChromeDriver 2.46 or ChromeDriver 71.0.3578.137
打开/etc/hosts文件,检查一下里面是否有127.0.0.1和localhost这两个内容是匹配的。
确认你的 /etc/hosts 文件里有一行 127.0.0.1 localhost,并且这行没有被注释掉。我的一些同事遇到过这个问题,我在把这一行删掉后也能复现这个问题。把它加回来后,问题就解决了。
对于Linux系统
1. 首先检查一下你安装的Chrome浏览器是不是最新版本,输入命令"chromium-browser -version"。
2. 如果不是最新的,那就安装最新版本的Chrome,输入"sudo apt-get install chromium-browser"。
3. 从这个链接获取合适版本的Chrome驱动:http://chromedriver.storage.googleapis.com/index.html。
4. 解压下载的chromedriver.zip文件。
5. 把解压出来的文件移动到/usr/bin/目录,输入sudo mv chromedriver /usr/bin/。
6. 进入/usr/bin/目录,然后需要运行类似于chmod a+x chromedriver的命令,让这个文件可以执行。
7. 最后,你就可以执行你的代码了。
import os
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome()
driver.get("http://www.google.com")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()