Checking a Box by Name and ID

Learn how to find and click on a checkbox using Selenium.

We'll cover the following

b## Check by name

We can click on a checkbox by its name attribute. For instance, in the below example, we first find our targeted checkbox by its name, and then perform a click operation on it:

driver.findElement(By.name("vehicle_bike")).click();

Check by ID

Similarly, we can also use the ID of the checkbox to click it. In the below example, we are finding our targeted checkbox by using its ID and then clicking on it (if it’s empty).

the_checkbox = driver.findElement(By.id("checkbox_car"));
if (!the_checkbox.isSelected()) {
    the_checkbox.click();
}

Get hands-on with 1200+ tech skills courses.