close

How to use browser that is already open and logged in with login credentials

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn aboutHow to use browser that is already open and logged in with login credentials in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to use browser that is already open and logged in with login credentials?

  1. How to use browser that is already open and logged in with login credentials?

     use folders as profiles and keep all logins persistent from session to session by using the Chrome options user-data-dir in order to use folders as profiles

  2. use browser that is already open and logged in with login credentials

     use folders as profiles and keep all logins persistent from session to session by using the Chrome options user-data-dir in order to use folders as profiles

Method 1

You can use pickle to save cookies as text file and load it after:

def save_cookie(driver, path):
    with open(path, 'wb') as filehandler:
        pickle.dump(driver.get_cookies(), filehandler)

def load_cookie(driver, path):
     with open(path, 'rb') as cookiesfile:
         cookies = pickle.load(cookiesfile)
         for cookie in cookies:
             driver.add_cookie(cookie)

With a script like:

from selenium import webdriver
from afile import save_cookie

driver = webdriver.Chrome()
driver.get('http://website.internets')

foo = input()

save_cookie(driver, '/tmp/cookie')

What you can do is:

  1. Run this script
  2. On the (selenium’s) browser, go to the website, login
  3. Go back to your terminal, type anything hit enter.
  4. Enjoy your cookie file at /tmp/cookie. You can now copy it into your code repo and package it into your app if needed.

So, now, in your main app code:

from afile import load_cookie

driver = webdriver.Chrome()
load_cookie(driver, 'path/to/cookie')

And you are now logged.

Method 2

 use folders as profiles and keep all logins persistent from session to session by using 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 manually interact at this step with the opened window and do the logins that check for human interaction, check remember password etc I do this and then the logins, 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. Second time I run, with exactly the same code as above, all the settings, cookies and logins 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.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read