且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-05 19:23:28

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

This is what finally got it working for me.

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.

To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.

此外,如果您想为 selenium 设置单独的配置文件:将路径替换为任何其他路径,如果在启动时它不存在,chrome 将为它创建新的配置文件和目录.

Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.