4

I am trying to disable all cookies when starting up either the Chrome or Firefox browser. I have seen the examples on here but they're all in Java, and some of the Selenium code is different than it is for Python.

ChromeOptions options = new ChromeOptions();  
Map prefs = new HashMap();  
prefs.put("profile.default_content_settings.cookies", 2);  
options.setExperimentalOptions("prefs", prefs); 
driver = new ChromeDriver(options);  

I want to do the above, just in Python.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
ohbrobig
  • 939
  • 2
  • 13
  • 34

4 Answers4

6

For Firefox:

from selenium import webdriver

fp = webdriver.FirefoxProfile()
fp.set_preference("network.cookie.cookieBehavior", 2)

browser = webdriver.Firefox(firefox_profile=fp)

Source: the FAQ, a JS selenium cookie question, and the description of Network.cookie.cookieBehavior.

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
5

For Chrome after version 45, you would need to do this (@alecxe was right up til Chrome 45 I think):

selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})

driver = webdriver.Chrome(chrome_options=chrome_options)

The only meaningful change there is default_content_settings becomes default_content_setting_values.

Ryley
  • 21,046
  • 2
  • 67
  • 81
  • The property name seems to still be the same. Can you confirm that this still works? For me, it doesn't when using SeleniumLibrary for Robotframework (which uses Python internally). – jotrocken Sep 11 '18 at 16:28
  • Checked it myself, via Python this still works with Chrome version 67. – jotrocken Sep 12 '18 at 09:08
  • For everyone coming across the same issue: Setting "prefs" does not work when using headless chrome, because it does not expose [preference handling](https://bugs.chromium.org/p/chromedriver/issues/detail?id=1925). – jotrocken Sep 12 '18 at 10:39
4

It would be:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})

driver = webdriver.Chrome(chrome_options=chrome_options)

tested - worked for me (Chrome 45, selenium 2.47).

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    Does this block all cookies? I see that it has marked "Block sites from setting any data" but not "Block third-party cookies and site data" – ohbrobig Sep 03 '15 at 19:00
  • 1
    Thanks. So this helps (I had most of it right). It doesn't actually block ALL data. Firefox seems to be more straightforward, whereas chrome has alot more options when it comes to blocking cookies and tracking. I can't find anything about blocking third-party cookies in the chromedriver doc (which is quite sparse). – ohbrobig Sep 08 '15 at 21:52
  • The new third party cookie blocking key is `profile.cookie_controls_mode`, `0 = allow, 1 = block, 2 = incognito`. https://stackoverflow.com/a/67260066 – lsl Apr 26 '21 at 02:21
0

You only need to change there is {"profile.default_content_setting_values.cookies": 2} becomes {"profile.block_third_party_cookies": True}.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.block_third_party_cookies": True})

driver = webdriver.Chrome(chrome_options=chrome_options)