【发布时间】:2026-01-08 01:25:03
【问题描述】:
我想使用 Python 的网络驱动程序以默认配置文件启动 Chrome,以便 cookie 和站点首选项在会话中保持不变。
我该怎么做?
【问题讨论】:
标签: python selenium-chromedriver
我想使用 Python 的网络驱动程序以默认配置文件启动 Chrome,以便 cookie 和站点首选项在会话中保持不变。
我该怎么做?
【问题讨论】:
标签: python selenium-chromedriver
我用“Yoannes Geissler”的回答解决了我的问题。
就我而言,我的个人资料被命名为“个人资料 2”
我的代码:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
下面一行解决了我的问题:
options.add_argument('--profile-directory=Profile 2')
【讨论】:
options.add_argument('--profile-directory=Profile 2') 的情况下为我工作。无论如何,我想这对某些人来说会有所不同。也可能是因为您拥有多个个人资料。
这个答案很简单,不言自明。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://*.com/a/57894065/4061346")
正如@MadRabbit 所说,在地址栏中输入chrome://version/ 以查找您的Chrome 配置文件数据的路径。
C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
/Users/user/Library/Application Support/Google/Chrome/Default
所以您所要做的就是从配置文件路径中删除最后一部分 。Default
注意:确保您不要同时运行多个会话以避免出现问题。
【讨论】:
Default 是什么意思?
只是为了分享对我有用的东西。使用 default 的配置文件很复杂,chrome 总是崩溃。
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
通过这样做,Chrome 将创建文件夹 User Data 并将所有数据保存在我想要的位置,并且很容易将您的项目移动到另一台机器上。
【讨论】:
这解决了我的问题。 (最后去掉默认)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options 已弃用。请改用options
【讨论】:
from selenium import [...?]
这就是最终让它为我工作的原因。
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
要查找您的 chrome 配置文件数据的路径,您需要在地址栏中输入 chrome://version/。例如。我的显示为C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,要在脚本中使用它,我必须排除\Default\,所以我们最终只得到C:\Users\pc\AppData\Local\Google\Chrome\User Data。
另外,如果您想为 selenium 创建单独的配置文件:将路径替换为任何其他路径,如果它在启动时不存在,chrome 将为它创建新的配置文件和目录。
【讨论】:
/Default/ 终于奏效了。但现在我面临一个问题,当添加 cookie 时,chrome 实例打开,它已经登录了我的谷歌帐户,但它失败并出现异常,不工作