首页 > 解决方案 > Chrome Webdriver 不加载默认配置文件

问题描述

Selenium Chrome Webdriver 不会加载我的默认 Chrome 配置文件。

我已经尝试了许多其他 Stack Overflow 解决方案,包括更改路径(以及使用本地 Chrome 应用程序,它会给出“权限被拒绝”错误)。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Application Support/Google/Chrome/Default")
options.add_argument("--profile-directory=Default")

driver = webdriver.Chrome()
driver.get("https://tagmanager.google.com/#/home")

assert "Google Analytics" in driver.title
account_element = driver.find_element_by_css_selector("div.account.card div#149385038 table")
accountPublicId = driver.find_element_by_css_selector("div#149385038 table td.account__public-id")

结果还是一样;它只加载一个“裸”Chrome Webdriver,而不是加载本地默认配置文件(我每天都在工作中使用)。

更新:

我不知道如何或为什么,但现在,当我退出 Chrome 并通过 Python 脚本启动 Chrome 时,Google Chrome 会从我的个人资料开始,但不使用我在该个人资料中的 cookie。

我将看看是否可以使用options.add_arguments.

标签: pythonmacosseleniumgoogle-chromeselenium-chromedriver

解决方案


尝试这个:

从硒导入网络驱动程序

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\ProfilePath") ##profile path
w = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)

推荐阅读