在 selenium/python/ubuntu 中使用 chromedriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22476112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using chromedriver with selenium/python/ubuntu
提问by Saheb
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 chromedriveris present in location /usr/local/bin.
我已经检查了多次并且chromedriver出现在 location /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
回答by Tytus
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, i.e. that it can be executed:
此外,请确保位于 /usr/local/bin/ 中的 chromedriver 可执行文件具有适当的文件权限,即它可以被执行:
> chmod 777 /usr/local/bin/chromedriver
回答by Robert Siemer
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, chromedriveris definitively not in the PATH.
如果您没有看到版本,chromedriver则绝对不在 PATH 中。
I don't tell webdriver where to find chromedriverotherwise. – I use the Ubuntu package “chromium-chromedriver”, but it drops the binary in /usr/lib/chromium-browser/chromedriver, which is not in my PATH. So I put a soft link in /usr/bin.
我不会告诉 webdriver 在哪里可以找到chromedriver其他的。– 我使用 Ubuntu 包“chromium-chromedriver”,但它会将二进制文件放入/usr/lib/chromium-browser/chromedriver. 所以我在/usr/bin.
回答by Zamfir Stefan
Following the suggestion from https://askubuntu.com/questions/539498/where-does-chromedriver-install-toI was able to make it work like this:
按照https://askubuntu.com/questions/539498/where-does-chromedriver-install-to的建议,我能够让它像这样工作:
Installed the chromium-chromedriver:
sudo apt-get install chromium-chromedriverAdding the path to the selenium line:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
安装了chromium-chromedriver:
sudo apt-get install chromium-chromedriver将路径添加到 selenium 行:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
Note that this opens Chromium and not Chrome. Hope it was helpful.
请注意,这会打开 Chromium 而不是 Chrome。希望它有帮助。
回答by Dev
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. Below is the code for java -
您需要确保独立的 ChromeDriver 二进制文件在您的路径中或在 webdriver.chrome.driver 环境变量中可用,然后尝试使用该二进制文件的绝对路径。下面是java的代码 -
File chromeDriver = new File("/usr/bin/chromedriver");
System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
driver = new ChromeDriver();
回答by amritkrs
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. Give it a try.
这在 ubuntu 中对我有用,向 bashrc 添加路径不起作用。试一试。
回答by Javier Sorella
I have solved the issue in the following way:
我已经通过以下方式解决了这个问题:
Open a terminal and type
whereis chromedriver. In my case, I had the following output:chromedriver: /usr/local/bin/chromedriverCopy that path and edit your Webdriver instance like:
打开终端并输入
whereis chromedriver. 就我而言,我有以下输出:chromedriver: /usr/local/bin/chromedriver复制该路径并编辑您的 Webdriver 实例,例如:
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
That should be enough!
应该够了!
回答by Janarthanan Ramu
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”

