- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check a particular checkbox from a set of checkboxes in a page in Selenium with python?
We can check a particular checkbox from a set of checkboxes in a page in Selenium with the help of find_elements method. While working on any checkboxes, we will always find an attribute type in the html code and its value should be checkbox.
This characteristic is only applicable to checkboxes on that particular page and to no other types of UI elements like edit box, link and so on.
To retrieve all the elements with attribute type = 'checkbox', we will use find_elements_by_xpath() method. This method returns a list of web elements with the type of xpath specified in the method argument. In case there are no matching elements, an empty list will be returned.
After the list of checkboxes are fetched, if we want to count its total numbers, we need to get the size of that list. The size of the list can be obtained from the len() method of the list data structure.
We have to iterate through this list with the help of loop. Then choose any attribute for the checkbox like name, label, value and so on with the help of get_attribute() method. If this attribute value matches with our choice, we shall perform clicking that particular checkbox with the click() method.
Code Implementation for selecting a particular checkbox.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm") #to refresh the browser driver.refresh() # identifying the checkboxes with type attribute in a list chk =driver.find_elements_by_xpath("//input[@type='checkbox']") # len method is used to get the size of that list print(len(chk)) # get_attribute method is get the value attribute for i in chk: if i.get_attribute("value") == "Tutorialspoint": i.click() #to close the browser driver.close()
- How to check a checkbox in a page in Selenium with python?
- How to count the number of checkboxes in a page in Selenium with python?
- How to get the total number of checkboxes in a page using Selenium?
- How to get the screenshot of a particular element in the page in Selenium with python?
- How to count the number of occurrences of a particular text inside a table in a page in Selenium with python?
- How to get the screenshot of a particular section of the page like the logo of a website in Selenium with python?
- How to select a radio button in a page in Selenium with python?
- Save a Web Page with Python Selenium
- Screenshot of a particular element with Python Selenium in Linux
- How to get the complete screenshot of a page in Selenium with python?
- How to get the values of a particular row in a table in Selenium with python?
- How to stop a page loading from Selenium in chrome?
- How to find the status of an element in a page in Selenium with python?
- How to count the total number of tables in a page in Selenium with python?