成功解决BUG:selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH.
异常解读
在使用 Python 操作 selenium 的时候,会出现如下错误:
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH
该错误翻译为中文是:
chromedriver.exe
文件没有找到,需要配置该文件对应的目录。
实际编码错误如下图所示。
解决思路
这个BUG是由于Selenium无法找到Chrome驱动程序(chromedriver)的可执行文件导致的。
解决方式只需要在代码中指定路径即可。
driver = webdriver.Chrome(r'G:\chromedriver_win32\chromedriver.exe')
错误复现
可以在 Python 文件中输入如下代码,即可出现本文标题所示错误:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
def get_data(url):
driver = webdriver.Chrome(r'G:\chromedriver_win32\chromedriver.exe')
# url = 'https://www.jd.com'
driver.get(url)
print(driver.title)
get_data('https://www.jd.com')
其他学习资料
- 《滚雪球学Python》专栏与实体书:https://dream.blog.csdn.net/article/details/131268344
- 《爬虫100例》:https://blog.csdn.net/hihell/category_9280209.html
- 《Python爬虫120》:https://blog.csdn.net/hihell/category_11079529.html