How to log in to a 2FA site, mobile BankID, using Selenium and Python?

Is is possible to use Selenium to log in to a site that uses a 2 factor autentication method (e.g bankID for a bank)?

driver = webdriver.Firefox(executable_path=r"C:Program Files (x86)geckodriver.exe")
# driver.implicitly_wait(2)
driver.maximize_window()

driver.get("theBankID-page")

try:
    personNumber = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.NAME, "personNumber")))

    personNumber.send_keys("personNumber")
    personNumber.send_keys(Keys.RETURN)

except TimeoutException:
    print("Failed to load BankID-page")

The above code makes you manually log in to the site using e.g. your phone as the second factor where you input the password.

But if I go to a new site, and the site is requesting a new log in with BankID when I go to a new page on the site, is it possible to store the log in session the first time? So it is persistent even when I go to a new page on the site.

Answer

Please check their official documentation: https://www.selenium.dev/documentation/en/worst_practices/two_factor_authentication/

It advises you to either disable it or if you can log in using another method that should work.

Leave a Reply

Your email address will not be published. Required fields are marked *