Selenium启动Chrome指定配置文件失败及ChatGPT登录异常求助
解决Selenium加载Chrome配置文件及ChatGPT登录验证问题
一、修复配置文件未加载的问题
- 修正路径错误:你代码中
--user-data-dir的路径是C:/User/USER/...,正确路径应为C:/Users/USER/...(注意User后多了一个s)。同时确认路径中的用户名和Profile 8名称完全匹配,可通过Chrome地址栏输入chrome://version/查看当前配置文件的实际路径。 - 关闭所有Chrome进程:若有Chrome窗口或后台进程在运行,Selenium无法加载被占用的配置文件。打开任务管理器,结束所有
chrome.exe进程后再运行代码。
二、解决浏览器“不安全”提示问题
Chrome会检测自动化工具,需添加以下参数规避检测:
options = webdriver.ChromeOptions() # 原有配置参数 options.add_argument('--user-data-dir=C:/Users/USER/AppData/Local/Google/Chrome/User Data') options.add_argument('--profile-directory=Profile 8') # 添加规避检测参数 options.add_argument('--disable-blink-features=AutomationControlled') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) service = Service("C:/Users/USER/Downloads/chromedriver-win64/chromedriver-win64/chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) # 隐藏webdriver特征 driver.execute_cdp_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
三、解决人机验证无限循环问题
- 确保配置文件已登录:手动打开对应Chrome配置文件,登录ChatGPT并确认会话有效,关闭浏览器后再用Selenium启动。
- 模拟真实用户行为:避免直接跳转ChatGPT,先打开普通网页停留并模拟操作,再访问目标站点:
from selenium.webdriver.common.action_chains import ActionChains import time # 先打开Google首页 driver.get("https://www.google.com") WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body'))) # 模拟鼠标移动 ActionChains(driver).move_by_offset(100, 100).perform() # 等待2秒 time.sleep(2) # 再打开ChatGPT driver.get("https://chat.openai.com/")
- 不要使用无头模式:若之前添加了
--headless参数,建议移除,无头模式更容易触发验证机制。
内容的提问来源于stack exchange,提问作者nekomin
相关产品推荐
相关产品推荐

