在selenium/python/ubuntu中使用chromedriver

2024-04-25 17:49:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用chromedriver执行一些测试,并尝试使用以下方法启动chromedriver。

driver = webdriver.Chrome('/usr/local/bin/chromedriver')

以及

driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

以及

import os
from selenium import webdriver

chromedriver = "/usr/local/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")

但这些似乎都没有帮助,错误是:selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.

我已经检查过多次了,chromedriver出现在位置/usr/local/bin

我的剧本还是没用。有人能帮忙吗。

我的google chrome位置是:usr/bin/google chrome


Tags: path方法importbinosusrlocaldriver
3条回答

以下各项正常工作:

driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

注意,在您的问题中,路径中没有前面的“/”。

此外,请确保位于/usr/local/bin/中的chromedriver可执行文件具有适当的文件权限,即它可以执行:

> chmod 777 /usr/local/bin/chromedriver

我已经用以下方法解决了这个问题:

  1. 打开终端并键入whereis chromedriver。在我的例子中,我有以下输出:

    chromedriver: /usr/local/bin/chromedriver

  2. 复制该路径并编辑Webdriver实例,如下所示:

driver = webdriver.Chrome('/usr/local/bin/chromedriver')

够了!

根据https://askubuntu.com/questions/539498/where-does-chromedriver-install-to的建议,我可以这样做:

  1. 安装铬铬驱动器:

    sudo apt-get install chromium-chromedriver
    
  2. 将路径添加到selenium行:

    driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
    

注意,这会打开铬而不是铬。希望能有所帮助。

相关问题 更多 >

    热门问题