Select CheckBox & Radio Button in Selenium WebDriver: In this automated world, it’s quite common that when trying to automate a Web Form, you can see that a web form contains Checkboxes and Radio buttons. These elements enhance the web page’s functionality by providing facilities like selecting single and multiple values.
Until this tutorial, we have seen how to locate the various types of elements with the help of different locator strategies. Still, in this tutorial, we will learn how to perform operations on Checkboxes and radio buttons. We need to verify a few new operations on these two elements, like if the checkbox is already checked or the radio button is already selected, and verify their default states.
How to Select CheckBox & Radio Button
We can learn the various possible ways to select the elements with real-time examples and verify the current status so that you can understand easily.
To select any element (Check Box / Radio button), you need to click on the element, so the command for this operation looks like below:
// Java code example to select checkbox/radio button. WebElement target = driver.findElement(By.id("checkbox1")); target.click();
When the ID Of Each radio Button Is Unique
package com.selenium.practice.checkboxRadiobutton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class RadiobuttonWithUniqueID { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkbox/Radio button have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); driver.findElement(By.id("python")).click(); System.out.println("Python Selected"); Thread.sleep(5000); driver.close(); } }
When Multiple Elements Have the Same ID
package com.selenium.practice.checkboxRadiobutton; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class RadiobuttonWithoutUniqueID { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkbox/Radio button have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); List<WebElement> checkBoxes= driver.findElements(By.xpath("//input[@id='checkbox']")); System.out.println("Select Ruby Radio Button: "+checkBoxes.size()); for(WebElement options : checkBoxes) { String linkName = options.getAttribute("value"); if (linkName.equals("Selenium")) { options.click(); break; // Exit from the loop, this is important } } Thread.sleep(5000); driver.close(); } }
How to Deal with CheckBox
package com.selenium.practice.checkboxRadiobutton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CheckBoxEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); //Select Singing CheckBox driver.findElement(By.id("sing")).click(); Thread.sleep(3000); System.out.println("Singing Check Box Selected"); driver.close(); } }
How do you check the state of the radio button or checkboxes?
Sometimes, before doing any action, we need to check the state of the Radio button or checkbox, and according to their state, we decide what operation we have to perform. So, to verify the element’s state, we can use the IsSelected() method. Executing the IsSelected() method will return a boolean value like true or false. Based on that, we can get to know the element’s state. If it is true, that means the element is selected; if it is false, that means that a specific element is not selected.
It’s better to verify a few things before performing any operation on the elements, like:
- If the Radio button or Checkbox is displayed on the webpage
- If the Radio button or Checkbox is enabled on the webpage
- Check the default selection of the Radio button or Checkbox
We can verify these things, as mentioned earlier, with the help of the below-predefined methods in Selenium webdriver:
isDisplayed(): This method returns a boolean value. if it returns true, the web element is displayed on the web page. If the element is not displayed on the web page, it will return a false Boolean value.
WebElement eleBtn = driver.findElement (By.id("testing")); eleBtn.isDisplayed();
package com.selenium.practice.checkboxRadiobutton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class IsDisplayedEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); //Locate The Element WebElement singing=driver.findElement(By.id("sing")); //If Element is Displaying Then Return True else It Will Return False Boolean CheckboxValue=singing.isDisplayed(); if(CheckboxValue) { System.out.println("Singing Is Displpaying On WebPage"); } else System.out.println("Singing Is Not Displaying In the Webpage"); driver.close(); } }
isEnabled(): This method returns a boolean value. This method is used to verify that a web element is enabled or disabled within a web page.
WebElement eleBtn = driver.findElement (By.id("testing")); eleBtn.isEnabled;
package com.selenium.practice.checkboxRadiobutton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class isEnabledEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); //Locate The Element WebElement singing=driver.findElement(By.id("sing")); //If Element is Enabled Then Return True else It Will Return False Boolean CheckboxValue=singing.isEnabled(); if(CheckboxValue) { System.out.println("Singing Is Enabled"); } else System.out.println("Singing Is Disabled"); driver.close(); } }
isSelected(): The isSelected() method is used to verify whether the other element is selected. If the element is selected, this method will return a true boolean value, and you will get a false value.
WebElement eleBtn = driver.findElement (By.id("testing")); eleBtn.isSelected();
package com.selenium.practice.checkboxRadiobutton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class isSelectedEx { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); //When The Checkboxes have an Unique ID driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); //Locate The Element driver.findElement(By.id("python")).click(); WebElement pythonElement=driver.findElement(By.id("python")); //If Element is Selected Then Return True else It Will Return False Boolean RadiobuttonValue=pythonElement.isSelected(); if(RadiobuttonValue) { System.out.println("Pyhton Is Selected"); } else System.out.println("Python Is Not Selected"); Thread.sleep(2000); driver.close(); } }