Complete the code to find a checkbox by its ID.
WebElement checkbox = driver.findElement(By.[1]("subscribeCheckbox"));
We use By.id to locate an element by its unique ID attribute.
Complete the code to check if the checkbox is selected.
boolean isChecked = checkbox.[1]();isDisplayed() which checks visibility, not selection.isEnabled() which checks if the element is enabled.The isSelected() method returns true if the checkbox is checked.
Fix the error in the code to click the checkbox only if it is not already selected.
if (!checkbox.[1]()) { checkbox.click(); }
isDisplayed() which checks visibility, not selection.isEnabled() which checks if the element is enabled.We use isSelected() to check if the checkbox is already checked before clicking.
Fill both blanks to find a checkbox by its name and click it if it is not selected.
WebElement checkbox = driver.findElement(By.[1]("agreeTerms")); if (!checkbox.[2]()) { checkbox.click(); }
id instead of name when the element is identified by name.isEnabled() instead of isSelected() to check selection.We find the checkbox by its name attribute and check if it is selected before clicking.
Fill all three blanks to find a checkbox by CSS selector, verify it is enabled, and then click it if not selected.
WebElement checkbox = driver.findElement(By.[1]("input[type='checkbox'][name='newsletter']")); if (checkbox.[2]() && !checkbox.[3]()) { checkbox.click(); }
id instead of cssSelector for complex selectors.isSelected() before isEnabled() which may cause errors.isChecked() which does not exist.We use cssSelector to find the checkbox, check if it is enabled and not selected before clicking.