How to click Radio button using selenium webdriver with Java?

In this post we are going to automate an Radio button by using click method from selenium webdriver.

Let’s use chropath to inspect Radio Button and identify the WebElement locator.

As it is visible that Radiobutton has id as “Business”. Let’s use this ID locator to click the radio button.

Click() : Click is an predefined method from Selenium webdriver class, where we can Select the radio button field.

Steps to click Radio Button using Selenium WebDriver with Java:

  1. Start writing code in an Java file.
  2. First let’s launch the browser (say using chrome here in this example).
  3. Enter the Appliction under test(AUT) url in the browser.

driver.get(“http://qacreators.com/FormToFill.html”);

  1. Maximize the browser window.

driver.manage().window().maximize();

  1. To identify the Radio button WebElement we are using By.id(“Business”) (i.e id locator)

Let’s discuss about this line of code :

WebElement radioButton = driver.findElement(By.id(“Business”));

–> driver : is the webdriver object

–> findElement() : Used to identify or locate the elements to perform the operation.

–> By.id(“Business”) : By.id helps the findElement() to find the elements with ID locator. Here in this example we are using (id = Business) to identify the radio button field.

–> click() : Webdriver command used to select radio button, identified using locator.

–> WebElement radioButton : Reference of the Radio Button element is stored into radioButton variable.

  1. To Select the Radiobutton element use click method.

radioButton.click();

  1. Execute the script by right click run as –> Java application.
  2. Once the script is executed we can observe the Radio button is Selected.
  3. Finally close the browser window.

Leave a Comment

Your email address will not be published. Required fields are marked *