c# - C# Selenium/ChromeDriver 添加用户配置文件首选项
问题描述
使用 selenium + chrome 驱动程序运行测试时,我需要允许所有 cookie。
我正在尝试使用ChromeOptions.AddUserProfilePreference将此添加为配置文件首选项
我不是 100% 确定允许所有 cookie 的首选项名称应该是什么。我已经引用了这个文档https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup
并在我的设置中尝试了以下操作,但没有达到预期的效果。
options.AddUserProfilePreference("profile.block_third_party_cookies", false);
options.AddUserProfilePreference("security.cookie_behavior", 0);```
这是我的设置代码
new DriverManager().SetUpDriver(new ChromeConfig());
var options = new OpenQA.Selenium.Chrome.ChromeOptions { };
options.AddArgument("–no-sandbox");
options.AddArguments("-disable-gpu");
options.AddArguments("-disable-dev-shm-usage");
options.AddArgument("-incognito");
options.AddArgument("-start-maximized");
options.AddUserProfilePreference("security.cookie_behavior", 0);
CurrentWebDriver = new ChromeDriver(options);
解决方案
我遇到了同样的问题。我发现使用以下内容对我有帮助:
options.AddUserProfilePreference("profile.cookie_controls_mode", 0);
帮助我找到这个的建议是检查 Chrome 首选项文件(在我的情况下C:\Users\<user>\AppData\Local\Google\Chrome\User Data\Default\Preferences
)。我保存了一个阻止 cookie 的副本,然后更改设置以允许所有 cookie 并比较两个版本,这突出了我受影响的控件。
推荐阅读
- android - 如何从“现有”android studio 项目创建 *.aar 文件(不创建新的 android 库)?
- java - 如果在我的给定 Java 代码上放置错误值并返回 ELSE 语句,如何修复循环
- mysql - mysql join和json输出
- php - 使用php中的文件上传进行用户注册
- node.js - Angular 7 HttpClient post请求未发送到nodejs服务器
- c# - 如何从 ASP.NET Core 中的请求标头或 url 读取会话 ID?
- rest - 如果在 Rest Assured / java 中包含“/”,我如何获取键的值?
- python-3.x - HTTPError:HTTP 错误 403:使用 smopy 时的 urllib3
- python - Django pass model to widget in ModelForm
- laravel - 如何配置路线,以便我的水疗中心从路径 /admin 开始?