【问题标题】:isSelected() not working for radio buttons in seleniumisSelected() 不适用于硒中的单选按钮
【发布时间】:2020-10-03 10:38:37
【问题描述】:

isSelected() 总是返回 false,即使单选按钮处于选中状态。

以下是未选中单选按钮时的 HTML

<input type="radio" class="chkbox" name="time1" value="3">

下面是我选择单选按钮时的 HTML

<input type="radio" class="chkbox" name="time1" value="3" checked> == $0

isSelected 在这两种情况下都返回 false

driver.findElement(By.name("time1")).isSelected()

我也尝试过使用 getAttribute("checked") 并且它返回 null

driver.findElement(By.name("time1")).getAttribute("checked")

谁能帮我获得单选按钮的选中状态!!!

【问题讨论】:

  • 我有同样的问题,但我认为我们不能在这里使用这个 isSelected 方法,因为它每次都返回相同的结果

标签: java html selenium-webdriver


【解决方案1】:

我正在使用以下解决方法来获取我的 HTML 的单选按钮选中状态

public boolean isRadioButtonSelected() {
        if(driver.findElement(By.xpath("//*[@name='time1' and @value='3']")).isDisplayed()) {
            return driver.findElement(By.xpath("//*[@name='time1' and @value='3' and @checked]")).isDisplayed();
        }
        return false;
    }

【讨论】:

    猜你喜欢
    • 2014-07-15
    • 1970-01-01
    • 2018-05-17
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2020-06-29
    相关资源
    最近更新 更多