0

I've a browser(not generated by selenium) which is already logged in to the website.

I need selenium to use the existing cookies so as to not prompt each generated browser for a new login.

here's what i've already tried from what i searched but still can't make it work.

code summary: if the selenium generated browser is already logged in, the browser will close and if it redirected to the login page, it would wait 60 seconds before closing.

import selenium.webdriver.support.ui as ui
import contextlib
import getpass

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options


websiteURL = "https://website.alreadylogged.in"
currentUser = getpass.getuser()

chromeOptions = Options()
chromeOptions.add_argument("--user-data-dir=C:\\Users\\" + currentUser + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path="C:\\Users\\"+currentUser+"\\Documents\\chromedriver.exe",options=chromeOptions)

with contextlib.closing(driver) as chromeDriver:    
    chromeOptions.add_argument("user-data-dir=C:\\Users\\" + currentUser + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default") 
    chromeDriver.get(websiteURL)
    wait = ui.WebDriverWait(chromeDriver, 60) # timeout after 60 seconds
    wait.until(lambda driver: chromeDriver.find_elements_by_class_name('      windows chrome      '))
user10888192
  • 27
  • 1
  • 7
  • Using a package like `pickle` can be a good way to dump your current session cookies and read them in again. They are dumped into a local file, so as long as they are still active, new sessions should be able to use them. See [this question](https://stackoverflow.com/questions/45417335/python-use-cookie-to-login-with-selenium) for an example – Brydenr Dec 09 '19 at 16:37
  • @Brydenr Hi, thank you for answering, but this only saves the cookies generated by selenium, i need the existing ones before the start of the automation. – user10888192 Dec 10 '19 at 01:38
  • You may be able to leverage https://stackoverflow.com/questions/31021764/where-does-chrome-store-cookies to get the cookies out of Chrome – Brydenr Dec 10 '19 at 15:24
  • Hi @user10888192, I have the exact same need. Did you manage to success to login with existing cookie session? – Gauthier Buttez Nov 17 '20 at 21:19

0 Answers0