In this post, we will learn how to check if a checkbox is in checked or unchecked state. Also, we will see how to check/set a checkbox if its not already checked.
In order to check if a checkbox is checked or unchecked, we can used the isSelected() method over the checkbox element. The isSelected() method returns a boolean value of true if the checkbox is checked false otherwise.
During automation, if we want to check a checkbox with click() method then the checkbox gets unchecked if its already checked. So, if you want to make sure that you are only checking the checkbox then we should first get the checkbox state using isSelected() method and then click on it only if it is in unchecked state.