4

I've requirement to test webpages using Selenium with below cookies settings

1. Allow sites to save and read cookie data (recommended)

2. Block third-party cookies

I found link how to disable cookies using webdriver for Chrome and FireFox JAVA, which does setting at global level and not individual setting under Cookies.

vikramvi
  • 3,312
  • 10
  • 45
  • 68

2 Answers2

8

It depends on the driver. With Chrome and Firefox you can set the behavior by setting the preferences with the driver.

Preferences with Chrome to allow/block the cookies:

// allow all cookies
"profile.default_content_setting_values.cookies": 1  // 1:allow, 2:block 
"profile.block_third_party_cookies": false

// block third party cookies
"profile.default_content_setting_values.cookies": 1  // 1:allow, 2:block
"profile.block_third_party_cookies": true

// block all cookies
"profile.default_content_setting_values.cookies": 2  // 1:allow, 2:block
"profile.block_third_party_cookies": true

Preferences with Firefox to allow/block the cookies :

// allow all cookies
"network.cookie.cookieBehavior": 0

// block third party cookies
"network.cookie.cookieBehavior": 1

// block all cookies
"network.cookie.cookieBehavior": 2

To figure-out a preference, simply compare the preferences before and after changing the option manually in the target browser.

For Chrome the file is named Preferences. To get the path of this folder, navigate about:config with Chrome.

For Firefox, the file is named prefs.js. To get the path of this folder, navigate about:profiles with Firefox.

Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Can you please clarify how you got the values profile.XXX for chrome browser ? – vikramvi Feb 13 '18 at 11:18
  • I got them from the preference file named `Preferences` which is present in the profile folder (see about:config for the path). Compare the file before and after changing the settings manually in chrome (Menu/Settings/Advanced/ContentSetting/Cookies). – Florent B. Feb 13 '18 at 14:08
  • Thanks for detailed info. – vikramvi Feb 13 '18 at 15:08
3

These setting have been migrated to a new key:

The new settings to block and manage third party cookies are:

// 1 = allow all cookies (default)
// 2 = block all cookies
"profile.default_content_setting_values.cookies": 1

// 0 = allow third party 
// 1 = block third party
// 2 = block third party in incognito (default)
"profile.cookie_controls_mode": 1
lsl
  • 4,371
  • 3
  • 39
  • 54