Unchecking and Asserting a Checkbox
Learn how to clear and assert a checkbox using Selenium.
We'll cover the following
Uncheck a checkbox
We can uncheck an already checked checkbox by using the following script:
the_checkbox = driver.findElement(By.name("vehicle_bike"));
the_checkbox.click();
if (the_checkbox.isSelected()) {
the_checkbox.click();
}
Assert if a checkbox is checked
We can assert whether a checkbox is checked or unchecked by the following script:
the_checkbox = driver.findElement(By.name("vehicle_bike"));
the_checkbox.click();
driver.sleep(100);
assert(the_checkbox.isSelected());
the_checkbox.click().then(function() {
driver.sleep(100);
the_checkbox.isSelected().then(function(selected) {
assert(!selected);
});
});
Get hands-on with 1400+ tech skills courses.