且构网

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

使用 Java 使用 Selenium WebDriver 加载 Chrome 配置文件

更新时间:2022-12-05 19:27:58

这是一个老问题,但我在让它工作时仍然遇到问题,所以我做了更多的研究以了解发生了什么.@PrashanthSams 的答案是正确的,但我错误地将 Default 添加到配置文件路径的末尾

This is an old question, but I was still having a problem getting it to work so I did some more research to understand what was happening. The answer from @PrashanthSams is correct, but I was incorrectly adding Default to the end of the profile path

我发现 Chrome 将 Default 附加到 user-data-dir 中指定的配置文件路径.因此,如果您的个人资料路径指定为:

I found that Chrome appends Default to the profile path specified in the user-data-dir. So if your profile path is specified as:

user-data-dir=C:Usersuser_nameAppDataLocalGoogleChromeUser DataDefault

它会附加 Default 并且你最终会在:

it will append Default and you will end up at:

C:Usersuser_nameAppDataLocalGoogleChromeUser DataDefaultDefault

这与您在该用户配置文件下正常打开 chrome 时获得的配置文件不同.

which is not the same as the profile that you would get if you opened chrome under that user profile normally.

如果您打开命令提示符、导航到 chrome 可执行目录并使用与此类似的指定选项运行 chrome,您还可以验证您的设置:

You can also verify your settings if you open a command prompt, navigate to the chrome executable directory, and run chrome with the options specified similar to this:

chrome.exe --user-data-dir="C:Usersuser_nameAppDataLocalGoogleChromeUser Data"

最后,您可以转到 Chrome 中的一个新选项卡并浏览到 chrome://version/,您将看到正在使用的实际配置文件.它将被列为:

Finally, you can go to a new tab in Chrome and browse to chrome://version/ you will see the actual profile that is being used. It will be listed like:

配置文件路径 C:Usersuser_nameAppDataLocalGoogleChromeUser DataDefault

Profile Path C:Usersuser_nameAppDataLocalGoogleChromeUser DataDefault