Action On CheckBoxes

We can use all types of locators to perform actions on checkboxes. Using the index and using checkboxes values we can do the required operation.

There are two ways to perform action on checkboxes.

  • Click by value
  • Click by Index

Click by Value

Here we are going to perform our action on the checkboxes using its value.


CheckBox


Below are the steps to perform action on the checkboxes.


1. find the list of checkboxes using any locator type.

checkbox_list = driver.find_elements(By.NAME,"checkbox")

2. Now Using for loop, iterate the list object (checkbox_list) and click on required option checkbox2.

3. Using is_selected() method checks whether the radio button is selected or not.

4. Wait for 5 sec and Close the Web Browser


CheckBox_by_Value.py

from selenium import webdriver
import time

from selenium.webdriver.common.by import By

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")

driver.get("http://www.dummypoint.com/seleniumtemplate.html")
time.sleep(2)

# 1. find the list of Checkboxes  using locator
checkbox_list = driver.find_elements(By.NAME,"checkbox")
time.sleep(2)

# 2. Using for loop iterate the list object and click on required option
for cbox in checkbox_list:
    cbox_v = cbox.get_attribute("value")
    print(cbox_v)
    if cbox_v == "c2":
        cbox.click()
        print("Is Selected : ",cbox.is_selected())
        break


time.sleep(5)
driver.quit()