Python webdriver 库未连接到 chromedriver -- "无法连接到服务 /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)

还有铬驱动程序:

$ 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的绝对路径。这是你自己的代码块将打开谷歌浏览器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)

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

相关内容

  • 没有找到相关文章

最新更新