代码改变世界

Selenium python+chrome/firefox报错Message: Can not connect to the Service chromedriver

2020-05-06 19:27  Tanwheey  阅读(1254)  评论(0编辑  收藏  举报

1、执行

>>> from selenium.webdriver import Chrome

#添加path后执行

>>> driver = Chrome()

或>>> from selenium.webdriver import Chrome

#驱动位置驱动

>>> Chrome(executable_path='/usr/local/bin/chromedriver')

报错:

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/cdtanghui3/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__

    self.service.start()

  File "/Users/cdtanghui3/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/common/service.py", line 92, 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

解决办法:加hosts:127.0.0.1 localhost

$ vi /etc/hosts

添加127.0.0.1 localhosts

执行:

>>> from selenium.webdriver import Chrome

>>> Chrome(executable_path='/usr/local/bin/chromedriver')

打开chrome浏览器成功