如何在 python 中使用 Selenium 单击 2FA 'Duo Push Button'

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

我正在尝试点击 2FA 'Send me a push' 按钮,但是 none 我之前的尝试都成功了

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()

这是我收到的错误

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)

我对 selenium 还是很陌生,并且正在学习。按钮是一种以特定方式运行的特殊类型的按钮吗?是否有访问按钮的特定方法?

向我发送推送检查:

文本为 给我发送消息 的按钮在


解决方案

因此,要点击 向我发送推送 ,您必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它

  • 诱导 所需的 元素可点击

  • 您可以使用以下任一项:

    driver.get("http://my.csus.edu")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='username']"))).send_keys("xxxxxxx")
    driver.find_element(By.XPATH, "//input[@id='password']").send_keys("xxxxxxxx")
    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()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照: