Running Selenium With Cookies Turned Off

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

How do I run a Selenium test with cookies off on the remote browser?

It depends on the browser.

For Chrome you need to add a chromeOptions entry to the capability you pass to Selenium.

caps = {}
caps['browser_api_name'] = 'Chrome39'
caps['os_api_name'] = 'WinXPSP2-C2'
caps['screen_resolution'] = '1024×768'
caps['record_video'] = 'true'
caps['record_network'] = 'false'
caps['record_snapshot'] = 'false'
caps['chromeOptions'] = { "prefs" : { "profile.default_content_settings.cookies" : 2 } }

For Firefox it is a little more involved. You need to create a profile object and pass that along in your capability you pass to Selenium.

profile = webdriver.FirefoxProfile()
profile.set_preference("network.cookie.cookieBehavior",2)
profile.update_preferences()
caps = webdriver.DesiredCapabilities.FIREFOX
caps['firefox_profile'] = profile
caps['browser_api_name'] = 'FF33'
caps['os_api_name'] = 'WinXPSP2-C2'
caps['screen_resolution'] = '1024×768'
caps['record_video'] = 'true'
caps['record_network'] = 'false'
caps['record_snapshot'] = 'false'

The extra information in the capabilty object signals.

See Also

Selenium and Python
Selenium and Ruby
Watir

Highlight search results