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

2024-03-29 04:47:01 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

这解决了我的问题。(结束时删除默认值)

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 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")

正如@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

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

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

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

相关问题 更多 >