WebDriver异常:消息:'无法连接到ChromeDriver'。在utils.is_connectable(self.port)中出现错误。

8

我正在尝试使用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")

如果路径错误,我会收到其他错误信息,但现在出错了,是在建立连接时。


你能重新格式化你的问题吗?现在它很难阅读。听起来你没有把chromedriver放在正确的路径下。你把它放在哪里了?你是如何指定位置的? - SiKing
/home/varunm/EC_WTF_0.4.10/EC_WTF0.4.10_Project/wtframework/wtf/drivers/chromedriver - kvm006
@SiKing,你现在明白我的问题了吗?感谢你的关注。我已经解决了路径问题。但现在出现了连接到chromedriver的错误。我的测试在Firefox上运行良好。 - kvm006
Linux系统有两个chromedriver库,分别适用于32位和64位系统。您是否使用了正确的库?您可以在此处下载它们:http://chromedriver.storage.googleapis.com/index.html?path=2.10/ - sap1ens
是的,我已经完成了64位的Chrome驱动程序...我的CentOS虚拟机也是64位的.. - kvm006
Chrome浏览器的版本是否重要?我已经有了最新版本的Chrome浏览器。 - kvm006
5个回答

21


针对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()

什么是pyvirtualdisplay?我在没有它的情况下运行代码,但chrome仍然无法运行。 - alexche8
@alexche8 我猜这是给那些在虚拟服务器上运行测试(没有显示器)的人使用的。 - kvm006
@alex,pyvirtuadisplay在内存中执行所有图形操作而不显示任何屏幕输出。请告诉我您的问题。 - Vicky
不得不使用一些选项 https://dev59.com/XFUL5IYBdhLWcg3wOl9x#60168019 - M at

19

请确认在您的/etc/hosts文件中添加并取消注释了以下行:127.0.0.1 localhost。这是一些同事遇到的问题,我在将此行删除后也能够重现该问题。将其添加回来后问题得以解决。


2
我的天啊,在我的情况下就是这样 - Adriano Varoli Piazza

4

打开 /etc/hosts 文件,检查 127.0.0.1 localhost 是否匹配


1
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

下载: http://chromedriver.chromium.org/downloads


0

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