Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium.common.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe_Python_Selenium_Selenium Webdriver_Webdriver_Selenium Chromedriver - Fatal编程技术网

Python selenium.common.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe

Python selenium.common.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe,python,selenium,selenium-webdriver,webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Webdriver,Selenium Chromedriver,我在本地有以下环境 铬67 Python 3.5.0 硒3.12.0 我已经下载了2.39版的chromedriver 我有如下.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

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

我已经下载了2.39版的chromedriver

我有如下.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
C:\Python354\python.exe D:/formf.py
回溯(最近一次呼叫最后一次):
文件“D:/PCPNDT/form.py”,第4行,在
driver=webdriver.Chrome(executable_path=“chromedriver.exe”)#可选参数,如果未指定,将搜索路径。
文件“C:\Python354\lib\site packages\selenium\webdriver\chrome\webdriver.py”,第68行,在\uuu init中__
self.service.start()
文件“C:\Python354\lib\site packages\selenium\webdriver\common\service.py”,第104行,在开始处
引发WebDriverException(“无法连接到服务%s”%self.path)
selenium.common.exceptions.WebDriverException:消息:无法连接到服务chromedriver.exe
我还尝试了其他网络驱动程序,如geckodriver.exe,但仍然存在相同的错误

请帮助我解决此错误


谢谢

您在可执行文件地址中出错:

driver = webdriver.Chrome(executable_path="hromedriver.exe")
应该是:

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

乍一看您的代码试用版,参数可执行路径的值似乎有一个小错误。而不是
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启动/生成
chromedriver服务

错误的潜在原因可能是:

  • 由于缺少
    /etc/hosts
    中的条目
    127.0.0.1 localhost
解决方案
  • Windows操作系统-将
    127.0.0.1本地主机添加到
    /etc/hosts

  • Mac OSX-确保以下条目:

    127.0.0.1   localhost
    255.255.255.255 broadcasthost
    ::1 localhost
    fe80::1%lo0 localhost   
    
工具书类 根据中的讨论:

  • Selenium不要求在主机文件中显式设置
    127.0.0.1 localhost
  • 但是,必须将本地主机映射到IPv4本地环回(127.0.0.1)
  • 这种映射的机制不必总是通过hosts文件
  • 在Windows操作系统上,它根本不映射到主机文件中(解析本地主机由DNS解析程序完成)
TL;博士

如果您使用的是linux,您需要将下载的chrome驱动程序放入usr/local/bin,然后再试一次?太棒了!!成功了。我的chrome exe我将其重命名为“hromedriver.exe”,只是为了避免名称冲突,因为我添加了几个版本。我现在制作的/etc/hosts文件中没有任何条目,它完全可以正常工作。谢谢