【问题标题】:I want to check only one radio button i selected in a list of 7 radio buttons in Selenium我只想检查我在 Selenium 的 7 个单选按钮列表中选择的一个单选按钮
【发布时间】:2018-11-16 09:07:19
【问题描述】:

这里是 Html 代码:

label for="experience">Years of Experience:</label
input name="experience" type="radio" value="one">
input name="experience" type="radio" value="two">
input name="experience" type="radio" value="three">

.......

等等” 在此我想检查仅使用 Selenium 选择了 1 个单选按钮

@Test

WebElement val = driver.findElement(By.name("experience"));
List<WebElement>radios = val.findElements(By.xpath("//input[@type ='radio']"));

for(WebElement radio : radios) {

if(radio.getAttribute("value").contains("four")){
radio.click();
String bt = (radio.getAttribute("value") + "  " + radio.isSelected());
              Assert.assertEquals(bt, "four  true");
              System.out.println(bt);

    }  

通过这个我传递一个特定的值,我希望它自动检查单选按钮的 1 是否被选中。

谢谢

【问题讨论】:

  • 请格式化您的代码
  • 不是格式化相关的 HTML 而是使用基于文本的未编辑 HTML 更新问题以进行正确分析。

标签: html selenium


【解决方案1】:

您可以将xpath更改为//input[@type='radio' and @checked]以获取选中的单选按钮的List,并验证List的大小是否等于1。如果为1,则表示只有1单选按钮被选中。

【讨论】:

  • //input[@type='radio' and @checked] 试过用这个但效果不好:(
  • 这有什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
相关资源
最近更新 更多