Selenium login via microsoft

Question:

I am trying to automate some data scraping from an app. Whole scraping thing works just fine but the problem is that log in is through microsoft and it is also using MS authentication so as far as my knowledge goes, it cant be fully automated.

The thing is that I have to log in only if I’m accessing this app through selenium, and while accessing it manually there’s no need to log in (app just opens).

I’m wondering if there is a way to make selenium not ask me to log in every time?
(I’m using chromium driver)

Asked By: evoooo

||

Answers:

Set user profile, so you can login as that user.
Example:

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

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data- 
dir=C:pathtochromeuserdata") #e.g. C:UsersYouAppDataLocalGoogleChromeUser Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:pathtochromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
Answered By: Deepak Garud

While accessing the website manually as you are logged in by default you can avoid logging in everytime by storing the cookies during first time logging in using pickle and reuse during your next login attempts as follows:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pickle

driver.execute("get", {'url': 'https://example.com/'})
# login using valid credentials
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()
driver = webdriver.Chrome(service=s, options=options)
driver.execute("get", {'url': 'https://example.com/'})
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
driver.execute("get", {'url': 'https://example.com/'})
Answered By: undetected Selenium
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.