Home > Net >  How to click on 2FA 'Duo Push Button' using Selenium in python
How to click on 2FA 'Duo Push Button' using Selenium in python

Time:05-03

I am trying to click on a 2FA 'Send me a push' button, however none of my previous attempts have succeeded

def launch_login():
    #open login web page

    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get("http://my.csus.edu")
    #enter login

    driver.find_element(By.XPATH, "//input[@id='username']").send_keys("xxxxxxxx")
    driver.find_element(By.XPATH, "//input[@id='password']").send_keys("xxxxxxxx")
    login = driver.find_element(By.XPATH, "//button[contains(text(),'Login')]")
    login.click()
    #click duo push button

    driver.find_element(By.XPATH, "//button[starts-with(@class, 'positive auth-button') and contains(., 'Send Me a Push')]").click()

    return driver
driver = launch_csus()

This is error I am receiving

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[starts-with(@class, 'positive auth-button') and contains(., 'Send Me a Push')]"}
  (Session info: chrome=101.0.4951.41)

I am still pretty new to selenium and am learning as I go. Is the button a special type of button that behaves in a certain way? Is there a specific way to access the button?

Send me a push inspect:

Send me a push inspect

CodePudding user response:

The button with the text as Send Me a Push is within an <iframe>

duo_iframe


Solution

So to click on Send Me a Push you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    driver.get("http://my.csus.edu")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='username']"))).send_keys("davidquintanill")
    driver.find_element(By.XPATH, "//input[@id='password']").send_keys("Determined12.")
    driver.find_element(By.XPATH, "//button[contains(text(),'Login')]").click()
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='duo_iframe']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Send Me a Push']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

idp

  • Related