首页 > 解决方案 > 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);

标签: c#seleniumselenium-chromedriver

解决方案


我遇到了同样的问题。我发现使用以下内容对我有帮助:

options.AddUserProfilePreference("profile.cookie_controls_mode", 0);

帮助我找到这个的建议是检查 Chrome 首选项文件(在我的情况下C:\Users\<user>\AppData\Local\Google\Chrome\User Data\Default\Preferences)。我保存了一个阻止 cookie 的副本,然后更改设置以允许所有 cookie 并比较两个版本,这突出了我受影响的控件。


推荐阅读