0

I am automating some tasks at work, and I need to use Selenium to get data that is updated on an internal website.

I can open the website and view this data in Edge Browser from my account without any kind of authentication,

However, when opening the same website with EdgeDriver, I get prompted for MFA every time I run the code

I cannot automate MFA since I have to use my phone,

Is it possible to use the same credentials with EdgeDriver as with Edge Browser? Or to just use the Edge Browser directly in selenium instead?

Thanks,

Python code below:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.wait import WebDriverWait

import schedule
import time
from random import randint


def job():
    while True:
        print("started health checkin")
        # Instantiate the webdriver with the executable location of MS Edge
        browser = webdriver.Edge(r"C:\msedgedriver.exe")
        # Simply just open a new Edge browser and go to lambdatest.com
        sleep(2)
        #browser.maximize_window()
        sleep(2)
        browser.get('https://xxxxxxxxxxxxxxxxxxxxxxxxx')
        try:


            sleep(3)

            myElem_2 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/div[3]/div/ng-component/pbi-menu/button[3]')))
            # Entering the email address
            sleep(3)
            myElem_2.click()


        except TimeoutException:
            print("No element found")
            sleep(5)
            browser.close() 

def day_job():
    sleep(randint(10,1000))
    job()

job()

print("starting daily timer")
schedule.every().day.at("08:00").do(day_job)

while True:
    schedule.run_pending()
    time.sleep(60) # wait one minute
Mich
  • 3,188
  • 4
  • 37
  • 85

1 Answers1

0

You're essentially looking for the Edge version of this previous answer that used Chrome: https://stackoverflow.com/a/31063104/7058266, for this question: How to load default profile in Chrome using Python Selenium Webdriver?

To avoid doing the 2-factor auth again if you already did it, reuse the profile.

Here's how to make it work for Edge:

from selenium import webdriver
from selenium.webdriver.edge.options import Options

options = Options() 
options.add_argument("user-data-dir=PATH_TO_YOUR_EDGE_PROFILE")
driver = webdriver.Edge(options=options)

And then do everything else you were doing. You should probably make a copy of your user-data-dir before you start, just in case Selenium makes unwanted changes.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48