【问题标题】:How to load default profile in Chrome using Python Selenium Webdriver?如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?
【发布时间】:2026-01-08 01:25:03
【问题描述】:

我想使用 Python 的网络驱动程序以默认配置文件启动 Chrome,以便 cookie 和站点首选项在会话中保持不变。

我该怎么做?

【问题讨论】:

    标签: python selenium-chromedriver


    【解决方案1】:

    我用“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') 的情况下为我工作。无论如何,我想这对某些人来说会有所不同。也可能是因为您拥有多个个人资料。
    【解决方案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 配置文件数据的路径。

    • 在 Windows 中看起来像这样C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
    • 在 Mac 中看起来像这样/Users/user/Library/Application Support/Google/Chrome/Default

    所以您所要做的就是从配置文件路径中删除最后一部分 Default

    注意:确保您不要同时运行多个会话以避免出现问题。

    【讨论】:

    • 遇到类似问题并按照您的说明进行操作,但收到错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态码是:0
    • @Victor 检查这些链接 *.com/a/39998437/4061346 *.com/a/46027522/4061346 并告诉我哪一个解决了您的问题。
    • 您好 Youssof H,非常感谢您回复我,但我听从您的建议并添加了 chrome 二进制文件的路径,但仍然无法加载配置文件。我有以下两个问题,我的问题更详细一些,你介意看一下吗? *.com/questions/60048743/…*.com/questions/60031070/…
    • 从配置文件路径中删除最后一部分Default 是什么意思?
    【解决方案3】:

    只是为了分享对我有用的东西。使用 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 并将所有数据保存在我想要的位置,并且很容易将您的项目移动到另一台机器上。

    【讨论】:

    • 我遇到了类似的问题,按照您的说明进行操作,但收到错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态码是:0
    【解决方案4】:

    这解决了我的问题。 (最后去掉默认)

    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 [...?]
    • 我试过这个,但它对我不起作用。 @NilsZenker 你能看看我在*.com/questions/53589103/… 的问题吗?
    • 遇到类似问题并按照您的说明进行操作,但收到错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态码是:0
    【解决方案5】:

    这就是最终让它为我工作的原因。

    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 将为它创建新的配置文件和目录。

    【讨论】:

    • 谢谢,我好几个小时都找不到这个问题的答案,从路径中删除默认值终于可以了。
    • 我正在关注这一点,但 Chrome 仍然不会加载配置文件。我正在使用 WSL(Linux 的 windows 子系统)并使用 windows 浏览器。
    • 它打开默认配置文件,如果我想打开特定配置文件怎么办?
    • 这仅在我的 chrome 关闭时才对我有用。必须通过创建配置文件目录的副本并提供其路径而不是原始路径来解决此问题。否则,如果另一个 chrome 已经使用相同的配置文件打开,它就会给出正在使用的目录。
    • 从路径中删除 /Default/ 终于奏效了。但现在我面临一个问题,当添加 cookie 时,chrome 实例打开,它已经登录了我的谷歌帐户,但它失败并出现异常,不工作