【问题标题】:Python webdriver library isn't getting connected to chromedriver -- "Can not connect to the Service /usr/local/bin/chromedriver"Python webdriver 库未连接到 chromedriver --“无法连接到服务 /usr/local/bin/chromedriver”
【发布时间】:2017-06-17 11:54:28
【问题描述】:

只是一个简单的 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 linux python-3.x selenium selenium-chromedriver


【解决方案1】:

这是您问题的答案:

我在运行 Python 3.x 的 Windows 8 Pro 机器上对您的代码进行了简单检查,并做了两个小改动。首先,我已将参数类型 executable_path 添加到 chromedriver 的绝对路径中,它在我的最后工作正常。其次,直到&,除非我们调用和消费chromedriver服务,否则我们真的不需要通过$ chromedriver启动chromedriver,并且可以继续通过我们的代码引用我们机器中chromedriver的绝对路径。这是您自己的代码块将打开 Google 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)

如果这能回答您的问题,请告诉我。

【讨论】:

  • 您可以考虑更新我执行我的确切代码块时生成的错误堆栈跟踪吗?谢谢
猜你喜欢
  • 2022-01-16
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 2021-09-28
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多