In all the example we have tried in the tutorial so far, we have not performed any action on “Checkbox” or “Radio Buttons”, Well in this tutorial we’ll see them all. Ideally “Checkbox” or “Radio Buttons” can be identified by simple “ID attributes”. But while doing actual automation that’s not the only thing we want.
We need to check whether the “Checkbox” is already checked or that “Radio Buttons” is already selected by default or anything like that. So we will be going to perform selection methods on them:
- By Using ID for Selecting Checkbox/Radio button.
- By Using IsSelected Method to Check the State of Checkbox/Radio button.
- By Using Element Value for Selecting Checkbox/Radio button.
- By Using CssSelector for Selecting Checkbox/Radio button.
- By Using ID for Selecting Checkbox/Radio button.
If ID value is given in the description of the Radio Button/CheckBox and you want to click on it irrespective of its value, then this command will be like this:
WebElement radioBtn = driver.findElement(By.id("hemant")); radioBtn.click();
- By Using IsSelected Method to Check the State of Checkbox/Radio button.
With this method, you can select or deselect radio button or checkbox. IsSelected statement is also used to know that the element is selected or not
Example: consider there are 2 checkboxes and one is selected by default and you wanted to check the first one and click on the next
// Store all the elements of the same category in the list of WebLements List oRadioButton = driver.findElements(By.name("checkbox")); // Create a boolean variable which will hold the value (True/False) boolean bValue = false; // This statement will return True, in case of first Radio button is selected bValue = oRadioButton.get(0).isSelected(); // This will check that if the bValue is True means if the first radio button is selected if(bValue = true){ // This will select Second radio button if the first radio button is selected by default oRadioButton.get(1).click(); }else{ // If the first radio button is not selected by default, the first will be selected oRadioButton.get(0).click(); }
- By Using Element Value for Selecting Checkbox/Radio button.
You can select the radio button if they have the value attribute as well. Below is the sample code example for this:
List oCheckBox = driver.findElements(By.name("username")); // This will tell you the number of checkboxes are present int iSize = oCheckBox.size(); // Start the loop from first checkbox to last checkboxe for(int i=0; i < iSize ; i++ ){ // Store the checkbox name to the string variable, using 'Value' attribute String sValue = oCheckBox.get(i).getAttribute("value"); // Select the checkbox it the value of the checkbox is same what you are looking for if (sValue.equalsIgnoreCase("softwaretestingboard")){ oCheckBox.get(i).click(); // This will take the execution out of for loop break; } }
- By Using CssSelector for Selecting Checkbox/Radio button.
The last useful way of selecting the checkbox/Radio Button is by using CSS selector. You can use all the CSS selection method (ID & Tag, name & tag etc). Below is a sample example:
WebElement oCheckBox = driver.findElement(By.cssSelector("input[value='Software Testing Board']")); oCheckBox.click();
If you have any questions or concern you can write in comments or also, you can write us at https://softwaretestingboard.com/qna/ask