繁体   English   中英

python webdriver库未连接到chromedriver —“无法连接到服务/ usr / local / bin / chromedriver”

[英]Python webdriver library isn't getting connected to chromedriver — “Can not connect to the Service /usr/local/bin/chromedriver”

只是Python中的一个简单代码:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--user-agent={}".format(config.USER_AGENT))
driver = webdriver.Chrome("/usr/local/bin/chromedriver", 
chrome_options=chrome_options)

和chromedriver:

$ ls /usr/local/bin/chromedriver
/usr/local/bin/chromedriver

和:

$ chromedriver 
Starting ChromeDriver 2.29 on port 9515
Only local connections are allowed.

但是,当我运行python脚本时,它引发了一个无法连接到chromedriver的异常:

Traceback (most recent call last):
  File "main.py", line 98, in <module>
    driver = webdriver.Chrome("/usr/local/bin/chromedriver", chrome_options=chrome_options)
  File "/home/me123/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/home/me123/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 102, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/local/bin/chromedriver

如何解决?

这是您的问题的答案:

我在运行Python 3.x的Windows 8 Pro机器上对您的代码进行了简单检查,做了两个小改动。 首先,我已经添加了参数类型executable_path到chromedriver的绝对路径和它在我结束工作正常。 其次,除非&除非调用并使用chromedriver服务,否则我们实际上并不需要通过$ chromedriver启动chromedriver, $ chromedriver可以继续通过我们的代码在我们的计算机中chromedriver的绝对路径。 这是您自己的代码块,它将打开Goog​​le Chrome v59.0:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--user-agent={}".format(config.USER_AGENT))
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver", 
    chrome_options=chrome_options)

让我知道这是否回答了您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM