在 python 中使用 selenium 查找单选按钮

Finding Radio Button using selenium in python

我正在尝试使用 python 自动填写反馈表(实际上是出勤)。我能够遍历所有页面,但每个页面都有不同的值和“Present”的 id 它是一种动态

有没有办法在页面上找到“Present”单选按钮。我对 html

了解不多

P.S。我已经阅读了关于 Whosebug 的问题,但答案无法解决它仍然会给我错误

此 xpath 应该根据 Present 标签找到您的输入标签。

//span[text()='Present']/preceding::input

是class

//input[class='form-check-input ']

要单击文本为 Present 的元素,您需要引入 WebDriverWait for the element_to_be_clickable() and you can use either of the following based :

  • 使用 index:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='statusdesc' and text()='Present']//preceding::input[1]"))).click()
    
  • 使用属性:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='statusdesc' and text()='Present']//preceding::input[@class='form-check-input ' and @name='status']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC