7

I need to add cookies via Webdriver using Firefox with Python. It works in normal mode, but not in private mode.

from selenium import webdriver
capabities = webdriver.DesiredCapabilities.FIREFOX
capabities.update({"javascriptEnabled":True})
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference("browser.privatebrowsing.autostart", True)
driver = webdriver.Firefox(desired_capabilities=capabities, firefox_profile=firefoxProfile)
driver.get("http://httpbin.org/cookies")
driver.add_cookie({"name":"drag", "value": "lol", "domain": "httpbin.org"})
driver.get("http://httpbin.org/cookies")

No matter how many times I refresh the driver, cookies don't load. There is nothing present in document.cookie in console log. It works fine in Chrome(not tested in incognito), and Firefox(non private).

I am aware in selenium private mode is redundant and I have read this SO question and also this.

But I cannot change much of the code. I need this feature to set cookie even in private mode in Firefox.

Firefox 66.0.1

Geckodriver 0.23.0 ( 2018-10-04)

Python selenium 3.14.1

Edit 1

I have tested with chrome(incognito) and it seems working

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
capabities = webdriver.DesiredCapabilities.CHROME
capabities.update({"javascriptEnabled":True})
driver = webdriver.Chrome(desired_capabilities=capabities, chrome_options=chrome_options)
driver.get("http://httpbin.org/cookies")
driver.add_cookie({"name":"drag", "value": "lol", "domain": "httpbin.org"})
driver.refresh()
driver.get_cookies()

Chromium 73.0.3683.75 Built on Ubuntu , running on Ubuntu 18.04

ChromeDriver 2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d)

Python selenium 3.14.1

I could also find some issues with firefox in this answer but couldn't quite make sense out of it. The version of firefox used there is also old so there might be some changes in the present version.

Community
  • 1
  • 1
Ja8zyjits
  • 1,433
  • 16
  • 31
  • The whole point of private mode on firefox/incognito mode on chrome is that it doesn't use cookies. If you want to use cookies you need to use a normal browsing window – Josh Heaps Jun 17 '22 at 18:56
  • Would it be easier to do it the other way around? Open non-private Firefox in python Selenium, clear cache and erase cookies, then set cookies in the way that is already working for you? Is private mode a necessity for what you are trying to do, or can it be accomplished through other means? – David Dancey Aug 20 '23 at 09:56
  • 1
    @DavidDancey Iam did that back then as a work around. – Ja8zyjits Aug 21 '23 at 09:14

0 Answers0