D. Müller
D. Müller

Reputation: 3426

Selenium: Select RadioButton in Label by Value (or Label Text)

I'm new to Selenium and want to select a RadioButton inside a group of labels, see Screenshot. The problem is, that all <input> elements have the same name! So I have to select them by the value (or by the label text?)...

I'm using the Java API for Selenium.

enter image description here

** As 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>

Upvotes: 0

Views: 5795

Answers (3)

Krzysztof Walczewski
Krzysztof Walczewski

Reputation: 670

In Page Object Pattern it will be:

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

Upvotes: 0

murali selenium
murali selenium

Reputation: 3927

Good to know you have solution.. below one may also helps..

If you are trying to click on specific button, if it has value say 'ATR' you can build xpath (or css selector) simply //input[@value='ATR'] or //input[contains(text(),'ATR Bilgileri')]

off-course other ways also there to find required element..

Thanks, Murali

Upvotes: 1

D. M&#252;ller
D. M&#252;ller

Reputation: 3426

I got it to work with the following code!!!

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!");
}

Upvotes: 0

Related Questions