【问题标题】:selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browserselenium.common.exceptions.WebDriverException:消息:打开 chrome 浏览器时无法连接到服务 chromedriver.exe
【发布时间】:2018-06-06 18:52:17
【问题描述】:

我在本地有以下环境 铬 67 Python 3.5.0 硒 3.12.0

我已经下载了 chromedriver 2.39 版

我有 .py 文件如下

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="hromedriver.exe")
driver.get('http://www.google.com')
time.sleep(5)
search_box = driver.find_element_by_name('q')
search_box.send_keys('Python')
search_box.submit()
time.sleep(5)
driver.quit()

我收到以下错误。

C:\Python354\python.exe D:/formf.py
Traceback (most recent call last):
  File "D:/PCPNDT/form.py", line 4, in <module>
    driver = webdriver.Chrome(executable_path="chromedriver.exe")  # Optional argument, if not specified will search path.
  File "C:\Python354\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
    self.service.start()
  File "C:\Python354\lib\site-packages\selenium\webdriver\common\service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe

我也尝试过使用 geckodriver.exe 等其他 webdriver 仍然出现同样的错误。

请帮我解决这个错误。

谢谢!

【问题讨论】:

  • 如果你在linux下需要把下载的chrome驱动放到usr/local/bin下再试一次?

标签: python selenium selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

乍一看您的代码试用版似乎在 argument executable_pathvalue 中有一个小错误。而不是hromedriver.exe,它应该是:

# Windows OS
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
# Linux OS
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')

此错误消息...

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe

...暗示程序/脚本无法通过 chromedriver.exe 启动/生成 ChromeDriverService

错误的潜在原因可能是:

  • 由于缺少/etc/hosts中的条目127.0.0.1 localhost

解决方案

  • Windows 操作系统 - 将127.0.0.1 localhost 添加到/etc/hosts

  • Mac OSX - 确保以下条目:

    127.0.0.1   localhost
    255.255.255.255 broadcasthost
    ::1 localhost
    fe80::1%lo0 localhost   
    

参考

根据selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver中的讨论:

  • Selenium 不需要在主机文件中显式设置 127.0.0.1 localhost
  • 但是,必须将 localhost 映射到 IPv4 本地环回 (127.0.0.1)
  • 此映射机制不必总是通过 hosts 文件。
  • Windows 操作系统 系统上,它根本没有映射到 hosts 文件中(解析 localhost 由 DNS 解析器完成)。

TL;DR

How to reset the Hosts file back to the default

【讨论】:

  • 太棒了!有效。我的 chrome exe 我将它重命名为“hromedriver.exe”只是为了避免名称冲突,因为我添加了几个版本。我现在制作的 /etc/hosts 文件中没有任何条目,它绝对可以正常工作。谢谢
【解决方案2】:

你输入错误的可执行地址:

driver = webdriver.Chrome(executable_path="hromedriver.exe")

应该是:

driver = webdriver.Chrome(executable_path="chromedriver.exe")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多