selenium.common.exceptions.NoSuchElementException:消息:Python selenium 中没有此类元素错误,即使 ID 和名称存在于 HTML 中

selenium.common.exceptions.NoSuchElementException: Message: no such element error in Python selenium even though ID and name are present in HTML

我尝试使用 by_id、by_name 在 selenium python 中 select 单选按钮,但收到错误消息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".radio_1"}
  (Session info: chrome=76.0.3809.100)

我试过了element_by_id()element_by_name()

import selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://iiiindia.org.in/app#/auth')
aa= driver.find_element_by_id('radio_1').click()

在此处输入代码

点击单选按钮之前,需要等待它就绪(可点击):

wait.until(expected_conditions.element_to_be_clickable((By.ID, "radio_1")).click()

所需的元素是一个 Angular 元素,因此要 click() 在元素上你必须为 element_to_be_clickable() 引入 WebDriverWait 并且你可以使用以下任一解决方案:

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "md-radio-button#radio_1"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//md-radio-button[@id='radio_1']"))).click()
    
  • 注意:您必须添加以下导入:

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

You can find a detailed discussion in