How to Select Checkbox and Radio button with example:
- Using radio button we will be able to select only one option from the options available because multi radio buttons we can not select.
- Using checkbox, we can select multiple options of different check boxes.
Radio Button HTML code:
1 | <input type=”radio”> |
Checkbox HTML code:
1 | <input type=”checkbox”> |
- Using Click() method in Selenium we can perform the action on the Radio button and on Checkbox.
Example:
1 2 3 | Webelement maleRadioBtn = driver.findElement (By.id("gender-male-button")); maleRadioBtn.click(); |
Example: Perform click operation or Handle Checkbox Radio button in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Radio_Checkbox { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Male Radio btnlocation store into maleRadioBtn WebElement WebElement RadioBtn = driver.findElement(By.id("gen-male")); // Click on Radio Button RadioBtn.click(); // Cricket Checkbox location store into Checkbox WebElement WebElement Cricket = driver.findElement(By.id("cricket")); // Hockey Checkbox location store into Checkbox WebElement WebElement Hockey = driver.findElement(By.id("hockey")); // Select Multiple Checkboxes Cricket.click(); Hockey.click(); } } |
Before performing on the Radio buttons or checkboxes need to verify:
- Verify Radio button or Checkbox is displayed on the web page before perform any operation.
- Verify Radio button or Checkbox is enabled on the web page before perform any operation.
- Check the default selection of the Radio button or Checkbox before perform any operation.
Below Verify methods used in Radio Button & Checkbox:
- isDisplayed()
- isEnabled()
- isSelected()
isDisplayed():
- returns a Boolean value, if it returns true then said radio button ispresent on the web page or if returns False then radio button is not present on the web page.
1 2 3 4 5 | // Male Radio Button location store into maleRadioBtn WebElement WebElement maleRadioBtn = driver.findElement (By.id("gender-male")); // Radio button is present in webpage then return boolean value boolean status = maleRadioBtn.isDisplayed(); |
isEnabled():
- returns a Boolean value, if it returns true then said radio button isEnabled on the web page or if returns False then radio button is not present on the web page.
1 2 3 4 5 | // Male Radio Button location store into maleRadioBtn WebElement WebElement maleRadioBtn = driver.findElement (By.id("gender-male")); // Radio button is present in web page then return boolean value boolean status = maleRadioBtn.isEnabled(); |
isSelected():
- returns a Boolean value, if it returns true then said radio button isSelected on the web page or if returns False then radio button is not present on the web page.
1 2 3 4 5 | // Male Radio Button location store into maleRadioBtn WebElement WebElement maleRadioBtn = driver.findElement (By.id("gender-male")); // Radio button is present in web page then return boolean value boolean status = maleRadioBtn.isSelected(); |
Example of Verify Methods of Checkbox / Radio button:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Radio_Checkbox { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Rad Button/Checkbox location store into Rad_check_id WebElement Radio_Checkbox = driver.findElement(By.id("Rad_check_id")); // Rad Button/checkbox isDisplayed then return boolean value boolean isDisplayed_status = Radio_Checkbox.isDisplayed(); // Rad Button/checkbox isEnabled then return boolean value boolean isEnabled_status = Radio_Checkbox.isEnabled(); // Rad Button/checkbox isSelected then return boolean value boolean isSelected_status = Radio_Checkbox.isSelected(); } } |
Select All Checkboxes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class All_Checkbox_Select { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // All checkbox locations store into list using findelements List<WebElement> els = driver.findElements(By.xpath("//input[@type='checkbox']")); // Using for each loop store all checkboxes into el for (WebElement el : els) { // Verify if checkbox in not selected then click if (!el.isSelected()) { el.click(); } } } } |
How to Select Checkbox and Radio button with example and How to Select Checkbox and Radio button with example test How to Select Checkbox and Radio button with example