1

I am automating a website using selenium pyhton. When I go to my website using driver.get(), it goes to microsoft login screen and after login it require two factor authentication and after that it opens the actual website. Now the problem is when i add new script in my code and i want to check if it is working right, I have to run all the above steps for login and then run the new code.

It is really time consuming. I need suggestions for avoid login each time just to check if my next lines of code are working or not. I'll appreciate your help.

mishal
  • 19
  • 3

1 Answers1

2

Cookies

To avoid the login process you have two options, the first one involves saving cookies in a file and loading it when necessary.

url = 'https://www.microsoft.com'
driver.get(url)
# manually enter username and password
import pickle
pickle.dump(driver.get_cookies(), open("cookies.pkl","wb"))
driver.quit()

then restart the webdriver and run

driver.get(url)
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
driver.refresh()

User profile

Alternatively, you can load a user profile where you are already logged in. Here you find a step by step tutorial on how to create a user profile in Chrome.

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 2")

driver = webdriver.Chrome(..., options=options)
driver.get(url)
sound wave
  • 3,191
  • 3
  • 11
  • 29