tintin
tintin

Reputation: 5867

How to enable cookies in Chromedriver with Webdriver?

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

Upvotes: 8

Views: 30080

Answers (2)

DevB2F
DevB2F

Reputation: 5075

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

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32845

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.

Upvotes: 4

Related Questions