Chapter 6_2 :Checkbox and Radio button in selenium

In this Chapter, we will see how to identify the following form elements

  • Radio Button
  • Check Box

Radio Button

Performing radio button select operation is very easy if you want to select the radio button without checking if any of the button is selected by default.these button and checkbox can be accessed by click() method. The difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options.

Lets take an example of “”.

For selecting Flight Details as Round Trip.

Here radio button present in flight details format.when we inspect above element we get html code as.

we can write following code:

WebElement radiobutton=driver.findElement(By.cssSeLector

For selecting flight details as one way

we can write following code as:

WebElement radiobutton=driver.findElement(By.cssSeLector

Complete Code:

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class radiobutton 
 public static void main(String[] args) 
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
WebElement name = driver.findElement("userName"));
// get the webelement corresponding to password (passwordfield)
WebElement password = driver.findElement("password"));
// to click on submit button
WebElement submitbutton = driver.findElement(By.cssSelector
//to select radiobuttons
WebElement radiobutton=driver.findElement(By.cssSelector
WebElement radiobutton1=driver.findElement(By.cssSelector

Check Box

Switching of check box is done by using the click() method. For practise lets take an html example:

first checkbox
try again