[英]Installing Chromedriver for Windows - selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH
I'm trying to webscrape with Python and selenium, and I've run into an error like so:我正在尝试使用 Python 和 selenium 进行网络抓取,但遇到了如下错误:
Traceback (most recent call last):
File "C:/Users/You/your_code.py", line 5, in <module>
driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
File "C:\Users\You\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\You\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'executable.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
It seems like I haven't installed chromedriver for selenium properly.好像我还没有为 selenium 正确安装 chromedriver。 How do I install chromedriver properly for selenium?
如何为 selenium 正确安装 chromedriver?
There are a few ways you can go about this:您可以通过以下几种方法 go 关于此:
The easiest way I've found to install chromedriver is to use chocolatey .我发现安装 chromedriver 的最简单方法是使用Chocolatey 。 You can follow the instructions to install it here , and once it's installed, just run
choco install chromedriver
(as an administrator), and it should install chromedriver for your version of chrome.您可以按照说明在此处安装它,安装后,只需运行
choco install chromedriver
(以管理员身份),它应该会为您的 chrome 版本安装 chromedriver。
Then in your code, just remove the reference to chromedriver's path:然后在您的代码中,只需删除对 chromedriver 路径的引用:
Before:前:
driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
After:后:
driver = webdriver.Chrome()
If you don't want to use chocolatey, then follow these steps如果您不想使用巧克力,请按照以下步骤操作
chrome://version/
in Chrome, to check your current version. chrome://version/
,以检查您当前的版本。 As you can see it's version 89
for me:89
版:win_32
one:win_32
一个:chromedriver.exe
in the same folder as your Python program:chromedriver.exe
放在与 Python 程序相同的文件夹中:chromedriver.exe
:chromedriver.exe
: Before:前:
driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
After:后:
driver = webdriver.Chrome(executable_path='chromedriver.exe')
...and you should be good to go. ...你应该对 go 很好。
If you want to add chromedriver to the PATH so you don't have to worry about where chromedriver is each time you write a selenium program, then it'd be a good idea to just use chocolatey, because it should install it globally.如果您想将 chromedriver 添加到 PATH 中,这样您每次编写 selenium 程序时不必担心 chromedriver 在哪里,那么最好只使用巧克力,因为它应该全局安装。 It's also much easier, so go with that if you can.
它也容易得多,所以如果可以的话,go 就可以了。 However, you can still set chromedriver to the PATH manually, following these steps (for Windows):
但是,您仍然可以按照以下步骤(对于 Windows)手动将 chromedriver 设置为 PATH:
C:\bin
by runningC:\bin
创建一个目录cd /
...then: ...然后:
mkdir bin
...then: ...然后:
cd bin
...then: ...然后:
explorer .
This should open the folder in File Explorer:这应该在文件资源管理器中打开文件夹:
setx PATH "%PATH%;C:\bin"
You should get something like this:你应该得到这样的东西:
chromedriver -v
.chromedriver -v
验证设置。 You should get something like this:C:\Users\You>chromedriver -v
ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761})
If so, you're all done.如果是这样,你就完成了。
Instructions for adding to PATH were adapted from here .添加到 PATH 的说明改编自此处。 For other operating systems, see here .
对于其他操作系统,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.