赞
踩
本地电脑谷歌浏览器有好几个谷歌用户账号,因需求需要用selenium打开指定的谷歌用户,但是报错:selenium.common.exceptions.WebDriverException: Message: unknown error: failed to write prefs file。
浏览器打开chrome://version/,个人资料路径为:C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Profile 19
获取--user-data-dir参数(所有用户目录)和--profile-directory参数(具体用户的文件夹名称)
- # -*- coding: utf-8 -*-
- from time import sleep
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.chrome.service import Service
- options = webdriver.ChromeOptions()
- # 步骤1获取到的User Data路径
- options.add_argument('--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\')
- # 步骤2获取到的--profile-directory值
- options.add_argument('--profile-directory=Profile 19')
- executable_path = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"
- s = Service(executable_path)
- driver = webdriver.Chrome(service=s, options=options)
- driver.get("https://www.baidu.com/")
- sleep(3)
- driver.quit()

该写法是新的写法
- s = Service(executable_path)
- driver = webdriver.Chrome(service=s, options=options)
之前的写法是
driver = webdriver.Chrome(executable_path, options=options)
上述代码报错:
- selenium.common.exceptions.WebDriverException: Message: unknown error: failed to write prefs file
- Stacktrace:
- Backtrace:
- GetHandleVerifier [0x0025A813+48355]
- (No symbol) [0x001EC4B1]
- (No symbol) [0x000F5358]
- (No symbol) [0x00115183]
- (No symbol) [0x001152B5]
- (No symbol) [0x00112EAD]
- (No symbol) [0x00110579]
- (No symbol) [0x00140C55]
- (No symbol) [0x0014093C]
- (No symbol) [0x0013A536]
- (No symbol) [0x001182DC]
- (No symbol) [0x001193DD]
- GetHandleVerifier [0x004BAABD+2539405]
- GetHandleVerifier [0x004FA78F+2800735]
- GetHandleVerifier [0x004F456C+2775612]
- GetHandleVerifier [0x002E51E0+616112]
- (No symbol) [0x001F5F8C]
- (No symbol) [0x001F2328]
- (No symbol) [0x001F240B]
- (No symbol) [0x001E4FF7]
- BaseThreadInitThunk [0x76E46359+25]
- RtlGetAppContainerNamedObjectPath [0x777B7C24+228]
- RtlGetAppContainerNamedObjectPath [0x777B7BF4+180]
- (No symbol) [0x00000000]

网上寻找资料说是因为内存原因,需要复制出去,比如之前用户数据是在C盘里的,只需要复制到D盘里就好。
- # -*- coding: utf-8 -*-
- from time import sleep
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.chrome.service import Service
- options = webdriver.ChromeOptions()
- # 步骤1获取到的User Data路径
- options.add_argument('--user-data-dir=D:\\GooUserData')
- # 步骤2获取到的--profile-directory值
- options.add_argument('--profile-directory=Profile 19')
- executable_path = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"
- s = Service(executable_path)
- driver = webdriver.Chrome(service=s, options=options)
- driver.get("https://www.baidu.com/")
- sleep(3)
- driver.quit()

能够打开浏览器,但是cookie记录都不存在了,无法保留登录状态。需要改成debug,但是很麻烦,相关参考文章:https://blog.csdn.net/suntao1995/article/details/119559593
我已经切换其他的playwright工具,很用很多
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。