问题描述
限时送ChatGPT账号..我正在尝试使用 Python 和 selenium 进行网页抓取,但遇到了如下错误:
回溯(最近一次调用最后一次):文件C:/Users/You/your_code.py",第 5 行,在 <module> 中.driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')文件C:\Users\You\lib\site-packages\selenium\webdriver\chrome\webdriver.py",第 73 行,在 __init__ 中self.service.start()文件C:\Users\You\lib\site-packages\selenium\webdriver\common\service.py",第 81 行,在开始引发 WebDriverException(seleniummon.exceptions.WebDriverException: 消息:'executable.exe' 可执行文件需要在 PATH 中.请参阅 https://sites.google/a/chromium/chromedriver/home
似乎我没有正确安装 selenium 的 chromedriver.如何为 selenium 正确安装 chromedriver?
解决方案有几种方法可以解决这个问题:
使用巧克力
我发现安装 chromedriver 的最简单方法是使用
转到 为您的操作系统下载正确的版本.例如,如果您使用的是 Windows,请下载win_32
一:解压 .ZIP 并将 chromedriver.exe
放在与 Python 程序相同的文件夹中:将 chromedriver 的路径更改为 chromedriver.exe
:之前:
driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
之后:
driver = webdriver.Chrome(executable_path='chromedriver.exe')
...你应该没事了.
将 chromedriver 添加到 PATH
如果你想将 chromedriver 添加到 PATH 中,这样你就不必担心每次编写 selenium 程序时 chromedriver 在哪里,那么最好使用 Chocolatey,因为它应该安装它全球.它也更容易,所以如果可以的话,就去吧.但是,您仍然可以按照以下步骤手动将 chromedriver 设置为 PATH(对于 Windows):
打开命令提示符.通过运行在C:\bin
创建一个目录cd/
...然后:
mkdir bin
...然后:
cd bin
...然后:
资源管理器.
这应该会在文件资源管理器中打开文件夹:
将 chromedriver.exe 放在此文件夹中通过在命令提示符中运行将其设置为 PATH:setx PATH "%PATH%;C:\bin";
你应该得到这样的东西:
关闭并重新打开命令提示符通过运行chromedriver -v
验证设置.你应该得到这样的东西:C:\Users\You>chromedriver -vChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761})
如果是这样,你就完成了.
添加到 PATH 的说明改编自 此处.对于其他操作系统,请参阅 这里.
I'm trying to webscrape with Python and selenium, and I've run into an error like so:
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(
seleniummon.exceptions.WebDriverException: Message: 'executable.exe' executable needs to be in PATH. Please see https://sites.google/a/chromium/chromedriver/home
It seems like I haven't installed chromedriver for selenium properly. How do I install chromedriver properly for selenium?
解决方案There are a few ways you can go about this:
Using Chocolatey
The easiest way I've found to install chromedriver is to use 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.
Then in your code, just remove the reference to chromedriver's path:
Before:
driver = webdriver.Chrome(executable_path='c:\path\to\windows\webdriver\executable.exe')
After:
driver = webdriver.Chrome()
Manually
If you don't want to use chocolatey, then follow these steps
Go tochrome://version/
in Chrome, to check your current version. As you can see it's version 89
for me:
Go to chromedriver.chromium/downloads and download the version of chromedriver that's the same as your browser version:
Download the right version for your OS. For example, if you are using Windows, download the win_32
one:
Extract the .ZIP and place chromedriver.exe
in the same folder as your Python program:
Change the path to chromedriver to just 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.
Adding chromedriver to the PATH
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. It's also much easier, so go with that if you can. However, you can still set chromedriver to the PATH manually, following these steps (for Windows):
Open Command Prompt. Create a directory atC:\bin
by running
cd /
...then:
mkdir bin
...then:
cd bin
...then:
explorer .
This should open the folder in File Explorer:
Place chromedriver.exe in this folder Set it to the PATH by running, in Command Prompt:
setx PATH "%PATH%;C:\bin"
You should get something like this:
Close and reopen Command Prompt Verify setup by runningchromedriver -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. For other operating systems, see here.
这篇关于为 Windows 安装 Chromedriver - seleniummon.exceptions.WebDriverException:消息:'chromedriver.exe' 可执行文件需要在 PATH 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论