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?
ChromeDriver has cookie be enabled by default.
Please try the following steps:
Use FirefoxDriver/IEDriver, see what the site says.
Manually open a Chrome browser, see what the site says.
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.
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
© 2022 - 2024 — McMap. All rights reserved.