'Unable to set browser cookies inside Selenium with Chromedriver

If I launch Chrome with Python Selenium like:

opts = Options()
opts.add_argument("user-agent=%s" % USER_AGENT)
opts.add_argument("--start-maximized")
opts.add_argument('--auto-open-devtools-for-tabs')
opts.add_experimental_option("excludeSwitches", ["enable-automation"])
opts.add_experimental_option('useAutomationExtension', False)
opts.add_experimental_option("prefs", {"credentials_enable_service": False, "profile.password_manager_enabled": False})
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=opts)

launch a window, neither Selenium, nor myself manually in the browser, can set cookies.

If I try:

driver.get('http://www.example.com')

I have some Javascript that retrieves the cookie value for mycookie, which reports the value:

abc

but then if I do:

driver.add_cookie({"name": "mycookie", "domain": "example.com", "value": "123"})

and then try to access it from Javascript, it reports the old abc value.

And if I manually use the console to try:

Cookies.set("mycookie", "123", {expires: 1});
Cookies.get("mycookie");

it still reports the old abc value.

Why am I unable to set cookie values with this Selenium configuration?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source