简体   繁体   English

Chromedriver可在路径中使用,但不能与硒一起使用(Python)

[英]Chromedriver working in path but not with selenium (python)

I've been attempting to use the selenium webdriver with Google Chrome. 我一直在尝试将硒webdriver与Google Chrome结合使用。 I have installed chromedriver, and set the path to it. 我已经安装了chromedriver,并设置了它的路径。 This has been done correctly as when I run chromedriver in terminal I get the output 这已经正确完成,因为当我在终端中运行chromedriver时,我得到了输出

Starting ChromeDriver 2.38.552518 
on port 9515
Only local connections are allowed

However, when I attempt to use the chromedriver on my python script: 但是,当我尝试在python脚本上使用chromedriver时:

from selenium import webdriver

driver = webdriver.Chrome()

I get this following error: 我收到以下错误消息:

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

I've tried multiple methods of specifying the direct path and also putting the chromedriver file in the folder the python script is in. But nothing seems to be working and they all give the same error! 我尝试了多种方法来指定直接路径,并将chromedriver文件放入python脚本所在的文件夹中。但是似乎没有任何效果,它们都给出相同的错误!

Please Help! 请帮忙!

edit: 编辑:

Path has been set in bash profile as such: 路径已在bash配置文件中设置为:

export PATH="~/drivers/:${PATH}"

Attempted Path specified in the python script as such: 尝试在python脚本中指定的路径,如下所示:

driver = webdriver.Chrome("~/drivers/chromedriver")

Still with the same error of: 仍然存在以下错误:

No such file or directory: '~/drivers/chromedriver': '~/drivers/chromedriver'

Java can't understand ~/drivers/chromedriver , Because only Linux Shell understand ~ is user home folder, But Java can't. Java无法理解~/drivers/chromedriver ,因为只有Linux Shell可以理解~是用户主文件夹,但是Java无法理解。

So your should use absolute path like /home/<userA>/drivers/chromedriver or relative path like ../drivers/chromedriver 所以,你应该使用这样的绝对路径/home/<userA>/drivers/chromedriver或类似的相对路径../drivers/chromedriver

您必须设置Path:

driver = webdriver.Chrome('C:/path/to/chromedriver.exe')

download selenium server-standalone -3.12.0.jar 下载硒服务器独立 -3.12.0.jar

and try this in a terminal: export CLASSPATH=".:selenium-server-standalone-3.12.0.jar " 并在终端中尝试: export CLASSPATH=".:selenium-server-standalone-3.12.0.jar

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

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