C#Selenium/ChromeDriver添加用户配置文件首选项 [英] C# Selenium/ChromeDriver Add User Profile Preference

查看:154
本文介绍了C#Selenium/ChromeDriver添加用户配置文件首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用硒+铬驱动程序运行测试时,我需要允许所有cookie.

I need to allow all cookies when running tests with selenium + chrome driver.

我正在尝试使用ChromeOptions.AddUserProfilePreference

我不确定100%允许所有cookie的首选项名称是什么.我已经引用了此文档 https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup

I'm not 100% sure what the preference name should be to allow all cookies. I have referenced this doc https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup

,并在我的设置中尝试了以下操作,但效果不理想.

and have tried the following in my setup but its not having the desired effect.

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);

推荐答案

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

I ran into the same issue. I found that using the following helped me:

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

帮助我找到此建议的建议是检查Chrome偏好设置文件(在我的情况下为 C:\ Users \< user> \ AppData \ Local \ Google \ Chrome \ User Data \ Default \ Preferences ).我保存了一个副本,其中阻止了cookie,然后将设置更改为允许所有cookie,并比较了两个版本,这对我来说是受影响的控件.

The advice that helped me find this, was to check the Chrome preferences file (in my case C:\Users\<user>\AppData\Local\Google\Chrome\User Data\Default\Preferences). I saved a copy with cookies blocked, then changed the setting to allow all cookies and compared the two versions, and that highlighted the affected control for me.

这篇关于C#Selenium/ChromeDriver添加用户配置文件首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆