簡體   English   中英

selenium.common.exceptions.WebDriverException:消息:打開chrome瀏覽器時無法連接到服務chromedriver.exe

[英]selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser

我的本地Chrome 67 Python 3.5.0 Selenium 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

我也嘗試過使用其他網絡驅動程序,例如geckodriver.exe仍然是相同的錯誤。

請幫助我解決此錯誤。

謝謝!

乍一看,您的代碼試用似乎在參數 execute_path中存在一個小錯誤。 代替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

......意味着程序/腳本無法啟動/產卵ChromeDriverService通過chromedriver.exe。

該錯誤的潛在原因可能是:

  • 由於缺少/etc/hosts的條目127.0.0.1 localhost

  • Windows操作系統 -將127.0.0.1 localhost /etc/hosts添加到/etc/hosts

  • Mac OSX-確保輸入以下內容:

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

參考

根據selenium.common.exceptions.WebDriverException中的討論:消息:無法連接到服務geckodriver

  • Selenium不需要在主機文件中顯式設置127.0.0.1 localhost
  • 但是,必須將本地主機映射到IPv4本地環回(127.0.0.1)
  • 這種映射的機制不必總是通過hosts文件。
  • Windows OS系統上,它根本沒有映射到hosts文件中(解析localhost由DNS解析器完成)。

TL; DR

如何將主機文件重置為默認值

您在可執行文件地址中犯了一個錯誤:

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

它應該是 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM