Hendi Ravasia
Hendi Ravasia

Reputation: 1

Selecting Radio button on Selenium Webdriver (Java)

I want to selecting one of radio button on such a test web application. after try and error i couldn't find out how to do it.

Here is the HTML of the web apps.

    <ol class="plain block-listing solid choice-area">
    <li class="qti-choice qti-simpleChoice"data-serial="choice_simplechoice_56c2a110874b8101352859" data-identifier="choice_1">
    <div class="pseudo-label-box">
    <label class="real-label">
    <input type="radio" value="choice_1" name="response-interaction_choiceinteraction_56c2a1108692f930922645">
    <span class="icon-radio"></span>
    </label>
    <div class="label-box">
    <div class="label-content clear" contenteditable="false">
    <div class="qti-block">a. Terminal</div>
    </div>
    </div>
    </div>
    </li>
    <li class="qti-choice qti-simpleChoice" data-serial="choice_simplechoice_56c2a110878f8127430456" data-identifier="choice_2">
    <div class="pseudo-label-box">
    <label class="real-label">
    <input type="radio" value="choice_2" name="response-interaction_choiceinteraction_56c2a1108692f930922645">
    <span class="icon-radio"></span>
    </label>
    <div class="label-box">
    <div class="label-content clear" contenteditable="false">
    <div class="qti-block">b. Pelabuhan</div>
    </div>
    </div>
    </div>
    </li>

On my selenium Webdriver, here is my code :

    WebElement choiceOption = driver.findElement(By.xpath("//html/body/div[1]/div[1]/div/div/div/ol/li[1]/div/label/span"));
    choiceOption.click();

Anyone can help? Thank you in advance.

Upvotes: 0

Views: 1393

Answers (5)

Sandeep
Sandeep

Reputation: 41

In a method first, find Webelement of ol tag, in ol find List of input tag and apply for loop for input tag list and match with value of the input tag to click

Try below code:

`

myMethod(String inputTagValue){
WebElement olTag = driver.findElement(By.tagName("ol"));
List<WebElement> inputTagList = olTag.findElements(By.tagName("input"));
for (WebElement item: inputTagList ) {
                if(item.getAttribute("value").equals(inputTagValue)){
                    item.click();
                    break;
                }
    }
}

`

Upvotes: 0

Radhika Kanle
Radhika Kanle

Reputation: 86

Instead of using an absolute path, you can go with relative path and there are multiple ways to locate this webelement. One of the simplest xpath would be : driver.findElement(By.xpath("//input[@value='choice_1']")).click();

Upvotes: 0

Prakash
Prakash

Reputation: 115

You can do it multiple ways.

  1. Get all option elements, loop through and click the one with the value you need.

    List options = driver.findElements(By.css("ol.plain.block-listing.solid.choice-area input") for(option in options){ if(option.value=="choice_2"){ if(!option.isChecked()) option.click(); break; } }

    }

  2. A simple way is, you can build a dynamic x-path or css with the value you wanted to click.. For example if choice_2 is what we wanted to click

    WebElement option = driver.findElement(By.css(input[value='choice_2'])); if(!option.isChecked()) option.click();

Hope that helps!

Upvotes: 0

Jyothishwar Deo
Jyothishwar Deo

Reputation: 436

Yes same answer

driver.findElement(By.xpath("//label[@class='real-label']//*[@value='choice_2']")).click();
//The above code selects the option "Pelabuhan"

Upvotes: 0

Bhumi Panchal
Bhumi Panchal

Reputation: 69

You can use input tag of radio button to select it Try this : WebElement choiceOption = driver.findElement(By.tagName("input")); choiceOption.click();

Upvotes: 0

Related Questions