简体   繁体   English

isSelected() 不适用于 selenium 中的单选按钮

[英]isSelected() not working for radio buttons in selenium

isSelected() always returning false even though radio button is in selected state.即使单选按钮在选定的 state 中,isSelected() 总是返回 false。

below is my HTML when radio button not selected下面是我的 HTML 未选中单选按钮时

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

And below is my HTML when radio button selected下面是我选择单选按钮时的 HTML

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

isSelected returning false for both the cases isSelected 在这两种情况下都返回 false

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

Also I had tried with getAttribute("checked") and it is returning null我也试过 getAttribute("checked") 并且它返回 null

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

Can someone help me to get radio button selected state!!!有人可以帮我获得单选按钮的选中状态!!!

I am using following workaround to get radio button selected state for my HTML我正在使用以下解决方法为我的 HTML 选择单选按钮 state

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM