How to identify multiple elements at the same time in Selenium with python?



We can locate and identify multiple elements at the same time in Selenium. There are various strategies to locate the elements. The different ways of locating elements are listed below −

  • find_elements_by_xpath – This method returns all the elements with matching xpath in the argument in form a list. It shall return an empty list if no element has the matching xpath.

Syntax

driver.find_elements_by_xpath("//input[name='text-search']")

find_elements_by_link_text – This method returns all the elements with matching value of link text in the argument to form a list. It shall return an empty list if no element has the matching text.

Syntax

driver.find_elements_by_link_text("Tutorialspoint")
  • find_elements_by_name – This method returns all the elements with a matching name attribute in the argument in form a list. It shall return an empty list if no element has the matching name.

Syntax

driver.find_elements_by_name("name-search")
  • find_elements_by_partial_link_text – This method returns all the elements with a matching partial value of link text in the argument to form a list. It shall return an empty list if no element has the matching name.

Syntax

driver.find_elements_by_partial_link_text("Tutorials") 
  • find_elements_by_tag_name – This method returns all the elements with a matching tag name in the argument in form a list. It shall return an empty list if no element has the matching tag name.

Syntax

driver.find_elements_by_tag_name("a")
  • find_elements_by_class_name – This method returns all the elements with a matching class name in the argument in form a list. It shall return an empty list if no element has the matching tag name.

Syntax

driver.find_elements_by_class_name("tutor")
  • find_elements_by_css_selector – This method returns all the elements with matching css in the argument in form a list. It shall return an empty list if no element has the matching css.

Syntax

driver.find_elements_by_css_selector("input#tutor")

Example

Coding Implementation for locating multiple elements.

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_automat ion_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)) #to close the browser driver.close()
Updated on: 2020-07-28T15:38:55+05:30

823 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements