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.

ALSO READ  Selenium Limitations

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.

ALSO READ  Page Object Model in Selenium WebDriver Step by Step Guide

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  How to take screenshot in Selenium IDE

ALSO READ Selenium Tutorials

Verify element is enable in Selenium

Rajesh

Rajesh has over 8+ years of experience in Quality Assurance (Automation/ Manual). I've completed his B.Texh. He is an experienced software tester,work experience include working on tools like TestNG, Sikuli, Autoit, Selenium Builder and Appium. Abhishek has worked on various projects from verticals like Retail, Health Insurance, tolling and many more and passionate about designing Automation Frameworks that are effective and easy to maintain using Selenium Java.

Leave a Reply

Your email address will not be published. Required fields are marked *