8

Am doing automation tesing using selenium, i need help in regarding how to select radio button.If possible help me with selenium java code.

1
  • How did answer provided by Tnem work for you? Care to accept as correct if it did? Commented Dec 10, 2012 at 12:41

7 Answers 7

7

Assuming you have selenium set up its just:

selenium.click('radio button locator');

You may want to look at the selenium javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

Sign up to request clarification or add additional context in comments.

1 Comment

The Selenium doc page mentioned is a good start but for a more comprehensive reference on locators see my wall chart: XPath, CSS, DOM and Selenium: The Rosetta Stone and Cookbook
1
click > xpath=(//input[@type='checkbox'])[position()=1]
click > xpath=(//input[@type='checkbox'])[position()=2]
click > xpath=(//input[@type='checkbox'])[position()=3]
click > xpath=(//input[@type='checkbox'])[position()=4]

etc ... use this commands to select random and any radio button

Comments

0
public class radioExamJavaScr {

   public static void main(String[] args) throws IOException {

     WebDriver driver = new FirefoxDriver();
     EventFiringWebDriver  dr = new EventFiringWebDriver(driver);
     dr.get("http://www.makemytrip.com/");
     dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

     ((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();");

     WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;");

     System.out.println(one_way.isSelected());

     WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;");

     System.out.println(round_trip.isSelected());

      }
 }

In the above example I am selecting radio button with "ROUND TRIP" using "JavaScript".

The last four lines are to verify and see whether the expected radio button is selected in the page or not.

NOTE: I am giving simple easy solution to solve a problem (selecting a radio) in the choosen webpage. A better code can be written. (user can write a method to accept radio ID and loop through all the existing radio button to see which one of them is selected).

1 Comment

The above solution works better if user unable to select radio button and verify with the help of " driver.findElement("") " method. I know few cases where "driver.findElement("")" and its actions unable to do the expected job (neither select nor verify). JavaScriptExecutor is second choice for any tester.
0

I use this method:

String radioButtonId = "radioButtonId";
selenium.focus("id=" + radioButtonId);
selenium.click("id=" + radioButtonId, "concreteRadioButtonValue");

Comments

0

What you can do is this:

Create a method with a WebElement return type, and use the Method findElement(). Example:

WebDriver test;
test = new FirefoxDriver();

public static WebElement checkAndGet(By b) throws Exception {
    return test.findElement(b);
    }

Store the WebElement and use the Method click(). Example:

WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']");
radiobutton.click();

Hope this helps!

Comments

0

Test Scenario : Select Gender(Female) radio button Steps:

  1. Launch new Browser
  2. Open URL http://toolsqa.wpengine.com/automation-practice-form/

  3. Select the Radio button (female) by Value ‘Female’

Code : public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(); // Step 1 - Launch Browser
    driver.get("http://toolsqa.com/automation-practice-form"); // Step 2 - Open Test URL
    List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex")); // 
    int size = rdBtn_Sex.size();
    System.out.println("Total no of radio button :"+size);
    for (int i=0; i< size; i++)
    {
        String sValue = rdBtn_Sex.get(i).getAttribute("value"); // Step 3 - 3.  Select the Radio button (female) by Value ‘Female’ 
        System.out.println("Radio button Name "+sValue);
        if (sValue.equalsIgnoreCase("Female"))
        {
            rdBtn_Sex.get(i).click();

        }
    }

Comments

-1

What you should do all the time is provide a locator for every object within a page and then use a method.

Like

driver.findElement(By.xpath(//CLASS[contains(., 'what you are looking for')])).click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.