简体   繁体   English

使用 selenium 打开多个镀铬轮廓

[英]open multiple chrome profile with selenium

when I run this code with chrome already open I get this error: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir当我在已打开 chrome 的情况下运行此代码时,出现此错误:消息:无效参数:用户数据目录已在使用中,请为 --user-data-dir 参数指定唯一值,或者不要使用 --user -数据目录

I need to have multiple profiles open at the same time, what can I do?我需要同时打开多个配置文件,我该怎么办?

 from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data") options.add_argument("--profile-directory=Profile 3") driver = webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options) driver.get("https://www.instagram.com")

Let's say you want to open two chrome profiles假设您要打开两个 chrome 配置文件

You need to instantiate two web drivers with the profile you want to set.您需要使用要设置的配置文件实例化两个 web 驱动程序。

From instantiate I meant, you need to create two chrome web drivers because once the options are set and you have created the driver , you cannot change this later从实例化我的意思是,您需要创建两个 chrome web 驱动程序,因为一旦设置了选项并创建了驱动程序,您以后就无法更改它

So,所以,

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

options = [Options(), Options()]
options[0].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options[1].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")

options[0].add_argument("--profile-directory=Profile 3")
options[1].add_argument("--profile-directory=Profile 4") # add another profile path

drivers = [webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[0]), webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[1])]

drivers[0].get("https://instagram.com")
drivers[1].get("https://instagram.com")

The user-data directory get locked by the first instance and the second instance fails with exception So the solution is to create a Userdata for each profile用户数据目录被第一个实例锁定,第二个实例因异常而失败所以解决方案是为每个配置文件创建一个 Userdata

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:\\anyPathWhereYouWantToSaveYourUserData\\profile1")
driver = webdriver.Chrome(executable_path=r'C:\Windows\System32\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")

No worries you can access your new profiles now manually if you would like to just by creating shortcuts you just need to add to your target in your shortcut不用担心,如果您只想通过创建快捷方式来手动访问您的新配置文件,您只需在快捷方式中添加到您的目标

"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\anyPathWhereYouWantToSaveYourUserData\profile1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM