Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we use local Chrome's cookies to login using Selenium?

I'm trying to create a test suite that does a login to Google's Gmail website with Selenium. The problem is that Selenium opens a new Chrome window (Like incognito mode) with no cookies. My code:

driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://hangouts.google.com/")

elem = driver.find_element_by_id("gb_70")
elem.send_keys(Keys.RETURN)

elem2 = driver.find_element_by_id("identifierId")
elem2.send_keys("[email protected]")
elem2.send_keys(Keys.RETURN)
time.sleep(2)

elem3 = driver.find_element_by_xpath("//*[@class='whsOnd zHQkBf']")
elem3.send_keys("myPass")
elem3.send_keys(Keys.RETURN)

From what I read on the internet it is possible to create 2 sessions, one for storing the cookies and the second for using those cookies. But this is not something that I'm looking for, I need it to use the cookies that Chrome store.

I tried to read the Chrome's cookies manually (from APPDATA) but it seems that it is encrypted by the browser.

like image 336
KaramJaber Avatar asked Dec 23 '22 09:12

KaramJaber


2 Answers

When you need cookies from session to session there is another way to do it, use the Chrome options user-data-dir in order to use folders as profiles, I run:

chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com")

You can do here the logins that check for human interaction, I do this and then the cookies I need now every-time I start the Webdriver with that folder everything is in there. You can also manually install the Extensions and have them in every session. Secon time I run, all the cookies are there:

chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com") #Now you can see  the cookies, the settings, Extensions and the logins done in the previous session are present here

The advantage is you can use multiple folders with different settings and cookies, Extensions without the need to load, unload cookies, install and uninstall Extensions, change settings, change logins via code, and thus no way to have the logic of the program break, etc Also this is faster than having to do it all by code.

like image 174
Eduard Florinescu Avatar answered Dec 28 '22 11:12

Eduard Florinescu


You need to do the following steps:

  1. Login as typical Gmail user and save cookies to the file (Script 1) under URL like (https://www.google.com).
  2. Login using cookies: open the browser in the needed domain (https://www.google.com) and load cookies, refresh the page (Script 2).

From your code:

  1. I think you need to add a wait after logging in.
  2. Check that cookies are set (Developer Tools -> Application -> Cookies).

PS: If you still have the same issue after -> change your URL to "https://accounts.google.com".

Hope it helps you!

like image 24
Ratmir Asanov Avatar answered Dec 28 '22 12:12

Ratmir Asanov