如何解决“chromedriver可执行文件需要在PATH中"在 PyDev 中使用 virtualenv 运行 Selenium Chrome 时出错? [英] How to resolve "chromedriver executable needs to be in PATH" error when running Selenium Chrome using virtualenv within PyDev?
问题描述
简短:
在 https://automatetheboringstuff.com/chapter11 阅读了使用 selenium 模块控制浏览器,我正在尝试在 PyDev 的虚拟环境中运行 Selenium Chrome 驱动程序.我已经设法从 PyDev 外部做到了,但从内部,我得到:
Having read Controlling the Browser with the selenium Module at https://automatetheboringstuff.com/chapter11, I am trying to run the Selenium Chrome driver in a virtual environment from PyDev. I have managed to do it from outside PyDev, but from within, I get:
selenium.common.exceptions.WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中.
长:
我使用的是 Linux Debian 3.10.11-1.
I'm using Linux Debian 3.10.11-1.
遵循 https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/,甚至在开始使用 PyDev 之前,我 ...
Following https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/, before even starting with PyDev, I . . .
使用 virtualenv 设置虚拟环境
Set up a virtual environment with virtualenv
安装 virtualenv
Installed virtualenv
pip install virtualenv
为我的项目创建一个目录
Made a directory for my project
cd ~/temp/
mkdir my_project
cd my_mproject
创建了一个名为env
virtualenv env
激活它.
Activated it.
source env/bin/activate
进入 Python 的交互模式,告诉自己我使用的是哪个版本的 Python
Went into Python's interactive mode to tell myself which version of Python I was using
python
Python 2.7.12rc1 (default, Jun 13 2016, 09:20:59)
[GCC 5.4.0 20160609] on linux2
退出交互模式并安装 Selenium 的东西
Exited out of the interacive mode and installed the Selenium stuff
首先是模块
First the module
pip install selenium
遵循 https://groups.google 上的建议.com/forum/#!topic/obey-the-testing-goat-book/Ty4FQoV3S0Q,安装了chromedriver
Following suggestion at https://groups.google.com/forum/#!topic/obey-the-testing-goat-book/Ty4FQoV3S0Q, installed chromedriver
cd env/bin/
wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
写了一个小脚本,看看它是否有效
Wrote a little script to see if it would work
from selenium import webdriver
driver = webdriver.Chrome()
运行它.Chrome 网络浏览器窗口打开.太好了.
Ran it. A Chrome web-browser window opened. Great.
现在在 PyDev 中尝试:
Now to try it in PyDev:
大致遵循 https://www.caktusgroup.com/博客/2011/08/31/getting-started-using-python-eclipse/...
已安装 Eclipse(Neon)
Installed Eclipse (Neon)
已安装 PyDev
从 Eclipse 中创建一个新项目(文件 菜单 > 新建项目 > 常规 > 项目strong>,输入项目名称并点击完成).
From within Eclipse, created a new project (File menu > New Project > General > Project, entered a Project Name and clicked Finish).
回到命令提示符(因为我还没有找到如何在 Eclipse 和 PyDev 中执行此操作):cd
进入我的新项目的根目录并创建一个虚拟环境.
Back at the command prompt (because I haven't yet found out how to do this from within Eclipse and PyDev): cd
'ed into my new project's root directory and created a virtual environment.
和以前一样,仍然在命令提示符下,我激活了新项目的虚拟环境并安装了 Selenium 模块,然后安装了 chromedriver_linux64 中出现的
文件.chromedriver
可执行文件.zip
As before, still at the command prompt, I activated the new project's virtual environment and installed the Selenium module and then the chromedriver
executable file that came up of the chromedriver_linux64.zip
file.
回到 Eclipse,我注册了我的项目以使用虚拟环境,我猜在 PyDev 术语中它不是虚拟环境,而是解释器:
Back in Eclipse, I signed up my project to use the virtual environment, which I guess in PyDev lingo is called not a virtual environment, but rather an interpreter:
Window 菜单 > 首选项 > PyDev > 解释器 > Python 解释器 > 添加.
Window menu > Preferences > PyDev > Interpreters > Python Interpreters > Add.
给解释器一个名字.
对于Interpreter Executable,我选择了项目虚拟环境bin
目录下的python2.7
文件
For Interpreter Executable, I selected the python2.7
file in my project's virtual environment's bin
directory
右键单击我的项目,选择属性 > PyDev - Interpreter/Grammar > 在Interpreter 下选择我的新解释器 >好的.
Right-clicked on my project, select Properties > PyDev - Interpreter/Grammar > Under Interpreter selected my new interpreter > OK.
给我的项目相同的脚本...
Gave my project the same script . . .
from selenium import webdriver
driver = webdriver.Chrome()
并通过点击运行菜单> 运行方式 > Python运行来运行它.
and ran it by clicking on the Run menu > Run As > Python Run.
现在,我只在 Eclipse 的控制台中收到一条消息,而不是打开 Chrome 网络浏览器窗口:
Now, though, instead of a Chrome web-browser window opening, I get only a message in Eclipse's console:
selenium.common.exceptions.WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中.
当我从命令提示符运行相同的脚本时,为了让 Web 浏览器窗口打开,我尝试过:
To get the web browser window to open as it does when I run the same scrip from a command prompt, I have tried:
将虚拟环境的
bin
文件夹(因为这是chromedriver
文件所在的位置)添加到解释器.
adding the the virtual environment's
bin
folder (because that's where thechromedriver
file is) to the interpreter.
删除解释器并创建新的解释器.
deleting the interpreter and creating it new.
将 chromedriver
复制到我的脚本所在的同一目录中.没有区别.
copying the chromedriver
into the same directory where my script is. No difference.
将我下载的 chromedriver_linux64.zip
文件添加到解释器中.还是一样的错误.
adding the chromedriver_linux64.zip
file that I downloaded to the interpreter. Still the same error.
我一直在 PyDev 中编写我的脚本,然后转向命令提示符来运行它.只是希望我能在 PyDev 的调试模式下运行它.
I been continuing by writing my script in PyDev, then turning to the command prompt to run it. Just wish I could run it in PyDev's debug mode.
如何在 PyDev 的PATH
"中获取这个chromedriver",以便我可以从 Eclipse 运行脚本?
How can I get this 'chromedriver' in the "PATH
" in PyDev so I can run the script from Eclipse?
推荐答案
如果将 chromedriver 放入 PATH 的所有尝试都失败了,您也可以像这样将可执行路径传递给 webdriver.Chrome():
If all attempts to put chromedriver in your PATH fail, you can also hand the executable path to webdriver.Chrome() like so:
chromedriver_loc = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=chromedriver_loc)
这是我尝试从 virtualenv 运行 chromedriver 时的最终解决方案.
This was my eventual solution when trying to run chromedriver from a virtualenv.
这篇关于如何解决“chromedriver可执行文件需要在PATH中"在 PyDev 中使用 virtualenv 运行 Selenium Chrome 时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!