How to enable cookies in Chromedriver with Webdriver?
Asked Answered
S

2

8

I am trying to use Selenium's Chromedriver to log in to a website which has cookies enabled. I got a message from the server that the browser has cookies disabled. How can I enable cookies in Chromedriver?

enter image description here

Spahi answered 6/10, 2013 at 16:16 Comment(3)
It should be enabled by default. Use your Selenium opened browser to whatismybrowser.com, see what the result is.Gnosis
Strangely whatismybrowser.com says that the cookies are enabled, but I get the message on the browser that the cookies are disabled(update the question with snapshot) I am trying to access ewf.companieshouse.gov.uk//seclogin?tc=1Spahi
@Spahi any updates on this?Lefler
G
4

ChromeDriver has cookie be enabled by default.

Please try the following steps:

  1. Use FirefoxDriver/IEDriver, see what the site says.

  2. Manually open a Chrome browser, see what the site says.

  3. Try open whatismybrowser.com with ChromeDriver opened browser, see what it tells you.

If your site says cookie not enabled for FirefoxDriver/IEDriver/Manually opened Chrome, and whatismybrowser.com says your ChromeDriver opened browser has cookie enabled, then clearly something is wrong with your site, please try debug your site.

Gnosis answered 7/10, 2013 at 20:34 Comment(1)
@tintin: Do you have any feedback on this? Did it work for you?Gnosis
I
0

What I did to use an instance of Chrome with Selenium where I was logged in was to run the following Python code, which opens a new instance of Chrome. If you already had Chrome open on your computer this will be a different instance of Chrome and you may see the Chrome app open in two different instances. With this new instance open I logged in to my account manually, then quit the new Chrome instance and ran the code again and then I was logged in.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "urlToWebsiteToLogin"
chrome_options = Options()
chrome_options.add_argument("user-data-dir=/Users/username/Library/Application Support/Google/Chrome/Default") #path for MacOS
chrome_options.add_experimental_option("detach", True) #prevent window from closing
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)

Remember to quit the Chrome instance before running the code again or you will get the following error:

invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

Ilonailonka answered 27/6, 2022 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.