问题
I have following environment at my local Chrome 67 Python 3.5.0 Selenium 3.12.0
I have downloaded chromedriver with version 2.39
I have .py file as follows
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()
I am getting following error.
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
I also tried with other webdriver like geckodriver.exe still same error.
Please help me out to resolve this error.
Thanks!
回答1:
At a first glance to your code trial it seems there is a minor bug in the value of the argument executable_path. Instead of hromedriver.exe it should have been:
# Windows OS
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
# Linux OS
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
This error message...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe
...implies that the the program/script was unable to initiate/spawn the ChromeDriverService through chromedriver.exe.
The potential reason for the error can be:
- Due to missing the entry
127.0.0.1 localhostin/etc/hosts
Solution
Windows OS - Add
127.0.0.1 localhostto/etc/hostsMac OSX - Ensure the following entries:
127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
References
As per the discussion in selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver:
- Selenium does not require
127.0.0.1 localhostto be explicitly set in the host file. - However it is mandatory requirement to map localhost to the IPv4 local loopback (127.0.0.1)
- The mechanism of this mapping does not have to be through the hosts file always.
- On Windows OS systems it is not mapped in the hosts file at all (resolving localhost is done by the DNS resolver).
TL;DR
How to reset the Hosts file back to the default
回答2:
You've made a mistake into the executable address :
driver = webdriver.Chrome(executable_path="hromedriver.exe")
It should be :
driver = webdriver.Chrome(executable_path="chromedriver.exe")
来源:https://stackoverflow.com/questions/50727595/selenium-common-exceptions-webdriverexception-message-can-not-connect-to-the-s