Selenium:按标签选择RadioButton(或标签文本)

时间:2015-12-14 09:44:43

标签: selenium selenium-webdriver selenium-ide

我是Selenium的新手,想要在一组标签中选择一个RadioButton,请参阅截图。问题是,所有<input>元素都具有相同的名称!所以我必须通过值(或标签文本?)选择它们......

我正在使用Selenium的Java API。

enter image description here

**作为HTML **

<table width="100%" border="1">
...
<label>
<input type="radio" name="AktarmaSekli" value="SP" checked class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Sipariş Planı</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="DF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">DAG Fatura Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="ATR" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">ATR Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AITM" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">AİTM Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="COC" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">CoC Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="Bakim" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Bakım Faturaları</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Araç Bilgileri</label></td></tr>
</table>

3 个答案:

答案 0 :(得分:1)

很高兴知道你有解决方案......下面也可以帮助..

如果你试图点击特定按钮,如果它有值'ATR'你可以简单地构建xpath(或css选择器)//输入[@ value ='ATR']或//输入[包含(文本) (),'ATR Bilgileri')]

偏离其他方式也可以找到所需元素..

谢谢, 穆拉利

答案 1 :(得分:0)

我让它使用以下代码!!!

private void selectRadioButtonByValue(WebDriver driver, String radioGroupName, String valueToFind) {
    List<WebElement> radioGroup = driver.findElements(By.name(radioGroupName));
     for (int i = 0; i < radioGroup.size(); i++) {
        if(radioGroup.get(i).getAttribute("value").equals(valueToFind)) {
            radioGroup.get(i).click();
            return;
        }
     }
     throw new org.openqa.selenium.NotFoundException("Element wasn't found by value!");
}

答案 2 :(得分:0)

在页面对象模式中,它将是:

@FindBy(xpath = "//*[contains(text(),'ATR Bilgileri')]")
WebElement radioATR;