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

Mad*_*bit 25 python selenium-chromedriver

因此,我想使用pythons webdriver打开其默认配置文件的chrome.我已经尝试了我能找到的所有东西,但我仍然无法让它发挥作用.谢谢您的帮助!

Mad*_*bit 56

这就是最终让它为我工作的东西.

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)
Run Code Online (Sandbox Code Playgroud)

要查找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将为其创建新的配置文件和目录.

  • 谢谢,我几个小时都找不到这个问题的答案,删除默认路径终于工作了. (6认同)
  • 仅当我的 chrome 关闭时,这才对我有用。必须通过创建配置文件目录的副本并提供其路径而不是原始路径来解决此问题。否则,如果另一个 chrome 已经使用相同的配置文件打开,则会给出正在使用的目录。 (2认同)
  • 从路径中删除“/Default/”终于成功了。但现在我面临问题,当添加 cookie 时,chrome 实例打开,其中已经登录了我的 google 帐户,但它失败并出现异常,不起作用 (2认同)

小智 21

我用“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)
Run Code Online (Sandbox Code Playgroud)

下面的行解决了我的问题:

options.add_argument('--profile-directory=Profile 2')
Run Code Online (Sandbox Code Playgroud)


Nil*_*ker 10

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

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")
Run Code Online (Sandbox Code Playgroud)

Chrome_Options不推荐使用.请options改用


Yoa*_*ler 6

只是分享对我有用的东西。使用默认的配置文件很复杂,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/")
Run Code Online (Sandbox Code Playgroud)

通过执行此操作,Chrome 将创建该文件夹User Data并将所有数据保存在我想要的位置,并且可以轻松地将您的项目移动到另一台计算机。


You*_* H. 6

这个答案非常简单且不言自明。

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://stackoverflow.com/a/57894065/4061346")
Run Code Online (Sandbox Code Playgroud)

正如 @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从配置文件路径中删除最后一部分。

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