Check if a checkbox is checked in selenium webdriver
In this current post, you will learn how to check if a checkbox is in checked or unchecked and we will learn how to check or set a checkbox if it’s not already checked or selected. WebDriver Interface provides with the following methods to check the visibility of the elements , web elements can be buttons, drop boxes, checkbox’s, radio buttons, labels etc.
TypeError can’t access dead object geckodriver
Verify if a checkbox is checked or not
In order to check/verify if a checkbox is selected or checked or unchecked or not selected, you can use isSelected() method for the checkbox element. This isSelected() method returns a boolean value ,if the checkbox is checked it returns true if not returns false.
isSelected()
Determine whether or not this element is checked or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.It returns boolean if checkbox or radio button is selected or checked.
Example Code:
boolean checkedStatus = driver.findElement(By.id("name")).isSelected(); System.out.println("The checkbox is selection state is - " + checkedStatus.isSelected());
Check a checkbox if Unchecked
public static void isElementSelected(WebDriver driver) { if (locatorType.equalsIgnoreCase("xpath")) { WebElement element = driver.findElement(By.xpath("checkbox")); if (element.isSelected() == true) { Reporting.testPassLogs(desc + " is selected "); System.out.println("Checkbox is already selected"); } else { System.out.println("Checkbox is not selected"); } } }
Please provide your valuable comments on this post and provide how this code is used in your projects..Thank you for reading my blog posts.
ALSO READ Selenium Tutorials