Selenium Chrome版本不匹配及webdriver-manager调用失败求助
问题解决方案
1. 初始代码的InvalidArgumentException修复
首先,你传入browser.get()的URL缺少协议头(https://),浏览器无法解析非法URL格式,这是报错的核心原因之一。同时代码遗漏了Service类的导入语句,需补充完整。
修正后的代码:
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service chrome_options = Options() chrome_options.add_experimental_option("detach", True) chrome_options.add_argument('--headless=new') chrome_options.add_argument('--no-sandbox') # 若本地ChromeDriver不在默认路径,需传入executable_path参数指定路径 service = Service() browser = webdriver.Chrome(service=service, options=chrome_options) browser.get('https://law.go.kr') time.sleep(5)
注:Chrome与ChromeDriver主版本(前三位)一致时,最后一位小版本差异不会引发兼容性问题,你推测的版本匹配问题并非本次报错的主要原因。
2. webdriver-manager的AttributeError修复
该错误表示webdriver-manager无法正确检测Chrome版本(返回None),可通过以下方式解决:
升级webdriver-manager:旧版本可能存在版本检测bug,执行命令更新:
pip install --upgrade webdriver-manager手动指定Chrome版本:自动检测失败时,直接传入你的Chrome版本号:
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome( service=ChromeService(ChromeDriverManager(version="122.0.6261.112").install()) ) driver.get('https://law.go.kr')指定Chrome安装路径:若webdriver-manager无法定位Chrome,手动指定二进制文件路径:
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager chrome_options = webdriver.ChromeOptions() # Windows路径示例,Mac/Linux需替换为对应路径 chrome_options.binary_location = "C:/Program Files/Google/Chrome/Application/chrome.exe" driver = webdriver.Chrome( service=ChromeService(ChromeDriverManager().install()), options=chrome_options ) driver.get('https://law.go.kr')
内容的提问来源于stack exchange,提问作者jiseongkim
相关产品推荐
相关产品推荐

