gpt4 book ai didi

Python webdriver 库未连接到 chromedriver -- "Can not connect to the Service/usr/local/bin/chromedriver"

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:10 31 4
gpt4 key购买 nike

只是一个简单的 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的绝对路径通过我们的代码在我们的机器中。这是您自己的代码块,将打开 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)

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

关于Python webdriver 库未连接到 chromedriver -- "Can not connect to the Service/usr/local/bin/chromedriver",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604305/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com