Selenium配置Chrome默认/指定配置文件失败问题排查
解决Selenium加载Chrome指定配置文件的问题
核心问题分析
你当前代码的问题在于user-data-dir路径设置错误——这个参数需要指向Chrome的用户数据根目录(即C:\\Users\\burak\\AppData\\Local\\Google\\Chrome\\User Data),而非具体的子配置文件夹;profile-directory才是用来指定具体配置文件夹(比如Person 1)的参数。此外,若有Chrome进程在后台运行,Selenium会因无法获取配置文件读写权限,自动进入访客模式。
修正后的代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # 先关闭所有Chrome进程再运行代码 options = webdriver.ChromeOptions() # user-data-dir指向Chrome用户数据根目录 options.add_argument(r'user-data-dir=C:\Users\burak\AppData\Local\Google\Chrome\User Data') # profile-directory指定要加载的具体配置文件夹 options.add_argument('profile-directory=Person 1') service = Service(r"C:\Users\burak\OneDrive\Masaüstü\Python\Scrap\chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) driver.get('https://www.google.com/')
额外注意事项
- 运行代码前,必须完全关闭所有Chrome窗口及后台进程(可在任务管理器中结束所有
chrome.exe进程),否则配置文件无法被正常读取。 - 确保
chromedriver.exe版本与你的Chrome浏览器版本完全匹配,版本不兼容也会导致配置加载失败。 - 若要加载默认用户配置,可将
profile-directory的值改为Default;自定义配置则直接使用对应的文件夹名称(如Person 1)。
内容的提问来源于stack exchange,提问作者Burak
相关产品推荐
相关产品推荐

