如何在 python 中使用 selenium 中预定义的 chrome 配置文件?

Yas*_*nif 1 python selenium python-3.x selenium-chromedriver

我的 Chrome 浏览器中有几个自定义配置文件。例如,我想在我的 selenium 中使用“test 2”配置文件。(见下图)

https://i.stack.imgur.com/iYcG8.png

这是我从互联网上看到的代码。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument(r'user-data-dir=C:\Users\mycomputer\AppData\Local\Google\Chrome\User Data')
browser = webdriver.Chrome(executable_path=r"C:\Users\mycomputer\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
Run Code Online (Sandbox Code Playgroud)

我如何使用“test 2”配置文件?谢谢。

小智 5

您只需要再添加一个参数:

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

Profile 1 - is a directory of your profile and its name is not like a profile name. You can explore all the profile direstories in your user-data-dir. How to find out what dir is the dir for "test 2" profile - you can find here https://superuser.com/a/723145