如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?

How to load default profile in Chrome using Python Selenium Webdriver?

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

我该怎么做?

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

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

这解决了我的问题。 (最后删除默认值)

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 代替

只是为了分享对我有用的东西。使用默认的配置文件很复杂,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
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("

正如@MadRabbit 所说,在地址栏中输入 chrome://version/ 以找到您的 chrome 配置文件数据的路径。

  • 在WindowsC:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
  • 中是这样的
  • 在Mac/Users/user/Library/Application Support/Google/Chrome/Default
  • 中是这样的

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

Note: Make sure you don't run more than one session at the same time to avoid problems.

我用“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')