Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Is there a way that for different runs of a python program that uses selenium I keep the browser that I have opened and logged in with my credentials, open and use in later runs?

I am debugging a code. On the browser each time I need to log in using my credentials. Currently, everytime I stop the code, the web-browser gets closed. Is there a way to keep a copy of browser that I have already open and logged in open and use it for my later debug so every time I don't need to enter my login credentials again?

My code that opens the browser looks like this:

driver = webdriver.Chrome(executable_path="/the_path/chromedriver", chrome_options=chrome_options) 
driver.get(url)

EDIT:

Actually, the way this website asks for authentication is as follows: First, it asks for the username, then I need to press the continue button, then it asks for the password, after entering the password, it sends an SMS to my phone, I need to enter it before it goes to the intended page.

like image 240
TJ1 Avatar asked Feb 20 '18 08:02

TJ1


People also ask

Can we use Selenium to work with an already open browser session?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

How do I run Selenium scripts on already opened browser Python?

You just need to modify your Selenium script to make Selenium connect to that opened browser. You can verify if the Chrome is launched in the right way: Launch a new browser window normally (without any flag), and navigate to http://127.0.0.1:9222.

Does your browser automatically log you in without typing your credentials?

Indeed, the browser knows your credentials, so why not automatically log you in as soon as you access the web site without even navigating to the login page? To be clear, users may see the login page only the first time. Then, they can log in without typing their credentials and without navigating to the login page.

How do I Make my browser automatically log me in?

- If you want your web browser to either automatically log you in or present you with a login prompt, enable authentication in your web server settings. Open Internet Explorer, select the Internet options menu item and open the Security tab of the popup window.

How to make the Windows authentication login prompts for user credentials?

How to make the windows authentication login prompts for user credentials in browsers? When the user logs on to the Dashboard Server using the Windows Authentication, the browser automatically detects the logged in Windows user, and authenticates to use the application. To log on with different users, enable the login prompt in browsers.

How to log on with different users in different browsers?

To log on with different users, enable the login prompt in browsers. The login prompt settings which are browser specific can be set as follows: Open the Internet Explorer and click the settings icon, and then select the ‘Internet Options’.


2 Answers

Well, since this question is upvoted but my flag as duplicated question wasn't accepted I will post here the same exact answer I already posted for a similar question:


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.

like image 186
Arount Avatar answered Oct 13 '22 00:10

Arount


This was a feature request and closed as not feasible. But is a way to do it, 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.

like image 32
Eduard Florinescu Avatar answered Oct 13 '22 01:10

Eduard Florinescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!