简体   繁体   English

在 selenium/python/ubuntu 中使用 chromedriver

[英]Using chromedriver with selenium/python/ubuntu

I am trying to execute some tests using chromedriver and have tried using the following methods to start chromedriver.我正在尝试使用 chromedriver 执行一些测试,并尝试使用以下方法启动 chromedriver。

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

and

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

and

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")

But none of these seems to help and the error is : selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.但这些似乎都没有帮助,错误是: selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.

I have checked multiple times and chromedriver is present in location /usr/local/bin .我已经检查了多次, chromedriver存在于/usr/local/bin

Still my scripts are not working.我的脚本仍然无法正常工作。 Could any body pls help.任何身体都可以帮忙。

My google-chrome location is : /usr/bin/google-chrome我的 google-chrome 位置是:/usr/bin/google-chrome

Following the suggestion from https://askubuntu.com/questions/539498/where-does-chromedriver-install-to I was able to make it work like this:按照https://askubuntu.com/questions/539498/where-does-chromedriver-install-to的建议,我能够让它像这样工作:

  1. Installed the chromium-chromedriver:安装了chromium-chromedriver:

     sudo apt-get install chromium-chromedriver
  2. Adding the path to the selenium line:将路径添加到 selenium 行:

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

Note that this opens Chromium and not Chrome.请注意,这会打开 Chromium 而不是 Chrome。 Hope it was helpful.希望它有帮助。

I have solved the issue in the following way:我已经通过以下方式解决了这个问题:

  1. Open a terminal and type whereis chromedriver .打开终端并输入whereis chromedriver In my case, I had the following output:就我而言,我有以下输出:

    chromedriver: /usr/local/bin/chromedriver

  2. Copy that path and edit your Webdriver instance like:复制该路径并编辑您的 Webdriver 实例,例如:

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

That should be enough!应该够了!

The following should normally work:以下应该正常工作:

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

Note that in your question there was no preceding '/' in the path.请注意,在您的问题中,路径中没有前面的“/”。

Additionally, make sure that the chromedriver executable located in /usr/local/bin/ has appropriate file permissions, ie that it can be executed:此外,请确保位于 /usr/local/bin/ 中的 chromedriver 可执行文件具有适当的文件权限,即它可以被执行:

> chmod 777 /usr/local/bin/chromedriver

As the message says: ChromeDriver executable needs to be available in the path.正如消息所说:ChromeDriver 可执行文件需要在路径中可用。

So is it in the path?那么它在路径中吗? What is the output of:什么是输出:

$ cd
$ chromedriver --version

If you don't see the version, chromedriver is definitively not in the PATH.如果您没有看到版本,则chromedriver肯定不在 PATH 中。

I don't tell webdriver where to find chromedriver otherwise.否则我不会告诉 webdriver 在哪里可以找到chromedriver – I use the Ubuntu package “chromium-chromedriver”, but it drops the binary in /usr/lib/chromium-browser/chromedriver , which is not in my PATH. – 我使用 Ubuntu 包“chromium-chromedriver”,但它会将二进制文件放到/usr/lib/chromium-browser/chromedriver ,这不在我的 PATH 中。 So I put a soft link in /usr/bin .所以我在/usr/bin放了一个软链接。

You need to make sure the standalone ChromeDriver binary is either in your path or available in the webdriver.chrome.driver environment variable and then try to use absolute path to that binary.您需要确保独立的 ChromeDriver 二进制文件在您的路径中或在 webdriver.chrome.driver 环境变量中可用,然后尝试使用该二进制文件的绝对路径。 Below is the code for java -下面是java的代码 -

    File chromeDriver = new File("/usr/bin/chromedriver");
    System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
    driver = new ChromeDriver();

Just pass the binary location as argument to it and not just the directory conatining it.只需将二进制位置作为参数传递给它,而不仅仅是包含它的目录。 So if it lies in /usr/bin directory, then run below command:因此,如果它位于 /usr/bin 目录中,则运行以下命令:

driver = webdriver.Chrome("/usr/bin/chromedriver")

This worked for me in ubuntu and adding path to bashrc is not working.这在 ubuntu 中对我有用,向 bashrc 添加路径不起作用。 Give it a try.试一试。

hope this will be useful for some who did like me.希望这对一些喜欢我的人有用。 For my case i left preceding slash in the path did "home/user/chromedriver" instead of "/home/user/chromedriver"对于我的情况,我在路径中留下了前面的斜杠是“home/user/chromedriver”而不是“/home/user/chromedriver”

For Mac users:对于Mac用户:

  1. brew install chromedriver . brew install chromedriver
  2. After installation ==> Linking Binary 'chromedriver' to '/usr/local/bin/chromedriver' will popup.安装后==> Linking Binary 'chromedriver' to '/usr/local/bin/chromedriver'将弹出。
  3. From now on, you should be able refer to /usr/local/bin/chromedriver in the code.从现在开始,您应该可以在代码中引用/usr/local/bin/chromedriver
  4. You might face Selenium Python: No such file or directory: '/usr/local/bin/chromedriver' but it exists and is added to path and/or Error message: "'chromedriver' executable needs to be available in the path" .您可能会遇到Selenium Python: No such file or directory: '/usr/local/bin/chromedriver' 但它存在并被添加到路径和/或错误消息: "'chromedriver' executable need to be available in the path" It's because brew install cask is required as well, as explained in https://www.kenst.com/2015/03/installing-chromedriver-on-mac-osx/ .这是因为brew install cask也是必需的,如https://www.kenst.com/2015/03/installing-chromedriver-on-mac-osx/ 中所述
  5. Again an error FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/bin/chromedriver' ?再次出现错误FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/bin/chromedriver' ? Try brew reinstall chromedriver and it was the first time brew reinstall chromedriver (got from Using Selenium on Mac Chrome ) returned something different than chromedriver not found .尝试brew reinstall chromedriver ,这是第一次brew reinstall chromedriver (从在 Mac Chrome 上使用 Selenium获得)返回的内容与chromedriver not found不同。 Namely, /usr/local/bin/chromedriver :)即, /usr/local/bin/chromedriver :)
  6. The last issue I was facing was “chromedriver” cannot be opened because the developer cannot be verified.我面临的最后一个问题是“chromedriver” cannot be opened because the developer cannot be verified. popup whenever I've tried to run the script.每当我尝试运行脚本时都会弹出。 cd /usr/local/bin , then xattr -d com.apple.quarantine chromedriver (credits: MacOS Catalina(v 10.15.3): Error: “chromedriver” cannot be opened because the developer cannot be verified. Unable to launch the chrome browser ) solves the problem, and the ChromeDriver finally ran. cd /usr/local/bin ,然后是xattr -d com.apple.quarantine chromedriver (来源: MacOS Catalina(v 10.15.3):错误:“chromedriver”无法打开,因为无法验证开发者。无法启动 chrome browser ) 解决了问题, ChromeDriver终于跑起来了。

Most probably you have not complete installing the chrome driver.很可能您还没有完成 chrome 驱动程序的安装。 I suggest you to install this with apt, Because apt installs all dependencies itself.我建议您使用 apt 安装它,因为 apt 会自行安装所有依赖项。 The other answers are true;其他答案是正确的; but in the last versions which installed "chromium-browser".但在安装“铬浏览器”的最新版本中。 but now this name is changed to "chromium-driver".但现在这个名字改为“chromium-driver”。 so you shoulde install this:所以你应该安装这个:

#apt-get install chromium-driver

this driver will be installed in /usr/bin and this name will be "chromedriver" so for import path to selenium use this path: /usr/bin/chromedriver:此驱动程序将安装在 /usr/bin 中,此名称将是“chromedriver”,因此对于 selenium 的导入路径,请使用此路径:/usr/bin/chromedriver:

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

It is enough to point out to the Chrome Browser driver on your machine.指出您机器上的 Chrome 浏览器驱动程序就足够了。 If you already have Chromium browser installed, then search for the driver:如果您已经安装了 Chromium 浏览器,请搜索驱动程序:

sudo find / -type f -name chromedriver

then select the driver corresponding to your browser.然后选择与您的浏览器对应的驱动程序。

PS: for me it was PS:对我来说是

driver = webdriver.Chrome('/snap/chromium/1827/usr/lib/chromium-browser/chromedriver')

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

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