如何解决Selenium WebDriver路径错误问题?
ChromeDriver运行报错问题排查及修复
问题根源分析
你的代码里存在三个关键问题:
- 路径转义错误:Windows系统下路径中的反斜杠
\是转义字符,直接写"D:\chromedriver.exe"会被解析成无效路径 - URL传递错误:
driver.get("url")里的"url"是字符串字面量,不是你定义的变量url - Selenium版本兼容性:如果你的Selenium是4.x版本,
executable_path参数已被弃用
修复后的代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 修复路径:用原始字符串避免转义 chrome_driver_path = r"D:\chromedriver.exe" # 也可以用双反斜杠写法:"D:\\chromedriver.exe" url = "https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=102257491&keywords=python%20developer&location=London%2C%20England%2C%20United%20Kingdom&redirect=false&position=1&pageNum=0" # Selenium 4.x版本推荐用法 service = Service(chrome_driver_path) driver = webdriver.Chrome(service=service) # 修复URL传递:直接使用变量,不要加引号 driver.get(url)
额外注意事项
- 确保ChromeDriver版本和你的Chrome浏览器版本完全匹配,版本不兼容是常见报错原因
- 检查D盘根目录下是否确实存在
chromedriver.exe,文件名不要有拼写错误
内容的提问来源于stack exchange,提问作者Alliyan Shahid
相关产品推荐
相关产品推荐

