C#Selenium使用其他用户个人资料启动Chrome [英] C# Selenium Start Chrome with Different User Profile
问题描述
在过去的两天里,我一直在尝试找到一种使用其他个人资料启动Chrome的方法,但无济于事. 不管我做什么,Selenium为chrome加载的配置文件始终是一些临时配置文件,例如"C:\ Users \ DARKBO〜1 \ AppData \ Local \ Temp \ scoped_dir14308_25046 \ Default"
For the past 2 days, I've been trying to find a way to start Chrome with a different profile but to no avail. No matter what I do, the profile that Selenium loads for chrome is always some temporary profile like "C:\Users\DARKBO~1\AppData\Local\Temp\scoped_dir14308_25046\Default"
我尝试了以下代码:
ChromeOptions options = new ChromeOptions();
options.AddArgument(@"user-data-dir=C:\SeleniumProfiles\Default");
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("chrome://version");
首先,我尝试直接从Chrome文件夹中使用配置文件的目录,但没有用.然后,我创建了一个新文件夹并将配置文件移到那里,我尝试在C:\和D:\中都这样做.没什么区别. 我试过像现在在代码中一样运行user-data-dir参数,并在它前面加上-.我试过使用不带@符号的双反斜杠,还是什么也没有.不管我做什么,配置文件目录始终是Selenium临时目录.
First I tried using the directories for the profiles directly from the Chrome folder, didn't work. Then I created a new folder and moved the profiles there, I've tried doing this both in C:\ and in D:\ . No difference whatsoever. I've tried running the user-data-dir argument both like it currently is in the code and with -- in front of it. I've tried using double backslashes without the @ symbol, still nothing. No matter what I do the profile directory is always the Selenium temp directory.
P.S.我通过chrome user-data-dir=C:\SeleniumProfiles
命令
P.S. 2:我的错误很简单,我忘记将选项放在新驱动程序的构造函数中.正如塔伦(Tarun)明确指出的那样,user-data-dir
仅给Chrome提供了包含配置文件的目录,然后我们需要使用profile-directory
参数提供包含所需配置文件的子目录.
P.S. 2: My mistake was very simple, I forgot to put the options in the constructor of the new driver. And as Tarun made it clear, user-data-dir
only gives Chrome the directory that contains the profiles, then we need to use profile-directory
argument to give the subdirectory that contains the needed profile.
推荐答案
您根本不使用options对象.
You din't use the options objects at all.
IWebDriver driver = new ChromeDriver();
应该是
IWebDriver driver = new ChromeDriver(options);
编辑1-Chrome配置文件和用户
Chrome浏览器具有用于存储配置文件的用户数据目录.在此目录中,可以维护多个配置文件.可以使用两个参数
Chrome has User data directory for storing profiles. Inside this directory multiple profiles can be maintained. There are two arguments that can be used
- 用户数据目录
- 个人资料目录
如果仅指定user-data-directory
,则将使用同一目录中的Default
目录.如果指定了profile-directory
,则使用user-data-directory
中的该目录
If only user-data-directory
is specified then a Default
directory inside the same would be used. If profile-directory
is specified then that directory inside the user-data-directory
is used
这篇关于C#Selenium使用其他用户个人资料启动Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!