Tuesday 7 January 2014

How to work with radio button in web driver

How to work with radio button in web driver?

 We can select the value from the drop down by using 3 methods.

selectByVisibleText - select by the text displayed in drop down
selectByIndex  - select by index of option in drop down
selectByValue  - select by value of option in drop down

<select id="44"> <option value="1">xyz</option>
 <option value="2">abc</option>
 <option value="3">pqr</option>
</select>

WebElement e = driver.findElement(By.id("44"));
Select selectElement=new Select(e);
// both of the below statements will select first option in the weblist
selectElement.selectByVisibleText("xyz"); 
selectElement.selectByValue("1");



8 comments:

  1. seams to be question is different and answer is different.

    ReplyDelete
    Replies
    1. correct...Answer is for selecting values from dropdown with normal type of dropdown. Here, answer for selecting values from DD of anchor tag is also missing.

      Delete
  2. what if multiple radio buttons are available on a webpage ?

    ReplyDelete
    Replies
    1. You will use contains to locate them

      Delete
  3. Hi Ruchi,
    Can you copy the sample code for check box, raiobutton, multiple select in drop downs code

    ReplyDelete
    Replies
    1. Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
      select a radio button with Selenium

      Delete
  4. WebElement e = driver.findElement(By.id("ID of the radio button"));

    e.click;

    // both of the below statements will select first option in the weblist

    selectElement.selectByVisibleText("xyz");

    selectElement.selectByValue("1");

    ReplyDelete