C# Selenium/ChromeDriver Add User Profile Preference

1,261 views
Skip to first unread message

Robert Sedgwick

unread,
Oct 11, 2020, 8:23:18 AM10/11/20
to Selenium Users
I need to allow all cookies when running tests with selenium + chrome driver.

I am trying to add this as a profile preference using ChromeOptions.AddUserProfilePreference

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

and

options.AddUserProfilePreference("security.cookie_behavior", 0);```


Here is my setup code


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

Jay Kay

unread,
Nov 27, 2020, 12:40:03 AM11/27/20
to Selenium Users
I posted an answer to this question on Stackoverflow (https://stackoverflow.com/a/65007362/8493196). The essence of which is that Chrome has changed the name of the related preference. You should now use:
    options.AddUserProfilePreference("profile.cookie_controls_mode", 0);  

The way I discovered it, was keeping copies of my own Preferences file both before and after making changes in my own Chrome session, and then comparing the two copies to see what changed.

The above has worked for me, setting the ChromeDriver browser to accept all cookies.

Reply all
Reply to author
Forward
0 new messages