Working with CheckBoxes and Radio Buttons

In the previous post, we have seen the working with WebList in selenium. In this post, we will see the working with CheckBoxes and Radio Buttons.

In our demo application, we have 3 checkboxes with color names Red,Green and White etc. We will see operations like select check box, deselect check box, choose radio button etc.

Check boxes


 

Uncheck the checkbox ‘Green’:

We will find a unique property of check box to make a XPATH. If we inspect check box using Firebug, then we observe that ‘Value’ attribute is having unique value among all check boxes. We will consider this to create the XPATH.

Green checkbox

 

We will use the method click() to uncheck the checkbox.

public class CheckBoxExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement CB_Green = driver.findElement(By.xpath("//input[@value='green']")); 
		CB_Green.click();

		}
}
Output: Check box ‘Green’ is unchecked.

 

Check the checkbox for color ‘Red’:

The click() method will be used to check the checkbox.

public class CheckBoxExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement CB_Red = driver.findElement(By.xpath("//input[@value='red']")); 
		CB_Red.click();

		}
}
Output: Check box ‘Red’ will be checked.

 

Selecting the radio button:

We have 2 radio buttons in application for ‘Male’ and ‘Female’. ‘Female’ radio button is by default selected. We need to select the radio button for ‘Male’.

Select radio button in selenium

 

We need to use method ‘Click()’ for selecting the radio button.

public class RadioBttnExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement RB_Male = driver.findElement(By.xpath("//*[@id='male']")); //radio button
		RB_Male.click();
		}
}
Output: Radion button for male will be selected.

Radio button in selenium


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...