Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

selenium - How to change Allow sites to save and read cookie data (recommended) and/or Block third-party cookies

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...