selenium指定谷歌用户,报错:Message: unknown error: failed to write prefs file

本地电脑谷歌浏览器有好几个谷歌用户账号,因需求需要用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工具,很用很多

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值