0

I'm setting a session with chrome in which is a need to log in the website with username, password and an OTP (one-time-Password) which is sent on the phone. The OTP is for browser only, which means if I enter a username, password and OTP in chrome once, next time it will ask me for username and password only not for OTP. I'm using selenium-python to automate this and selenium always open a new browser Window. How can I overcome this situation?

I tried to save the cookies and then get it to load on the driver but still asking for OTP.

# Saved functions are below.

import pickle

# Path = "E:\\Python\\cookies\\cookies.pkl".
def save_cookie(driver, path):
    """
    Save cookies in cookies folder.
    """
    with open(path, 'wb') as filehandler:
        pickle.dump(driver.get_cookies(), filehandler)

def load_cookie(driver,path):
    """
    Load cookies from the saved folder.
    """
    with open(path, 'rb') as cookiesfile:
        cookies = pickle.load(cookiesfile)
        for cookie in cookies:
            driver.add_cookie(cookie)

# Here is what I'm trying.

from selenium import webdriver
from udf import cookies
import pickle

driver = webdriver.Chrome()
driver.get('https://somewebsite.com/')
cookies.save_cookie(driver,'E:\\Python\\cookies\\cookies.pkl')

cookies.load_cookie(driver, 'E:\\Python\\cookies\\cookies.pkl')

driver.refresh()

I still need to submit OTP for the Chrome browser.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40

2 Answers2

0

Open chrome browser manually. Navigate to chrome://version/

Note down Profile Path, its profile presently you are using.

In same browser navigate to you application, do login with all required things.

then call this profile in script.

See this how to call in python How to load default profile in chrome using Python Selenium Webdriver?

You have to provide Profile Path here in above link

options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

below code worked for me.
Description: I had created a new profile in Chrome by following steps chrome://settings/> Add and Manage People > Add Person. Then, login to my youtube account in the new profile. Then following code gives me auto logged-in youtube page. Profile Path can be found in chrome://version/ in Profile Path: key.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\randh\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2") #Path to your chrome profile which you have created.
driver = webdriver.Chrome(chrome_options=options) #Place webdrivers in Scripts folder of Python(Where Python is installed), so that it can be picked automatically.
driver.get('https://youtube.com')

Like or comment...

  • If answer works for you you can mark it as accepted one (checkmark to the left of it) Even if answer is your own. – Guru Stron Feb 25 '23 at 10:05