Skip to content
Related Articles

Related Articles

Improve Article

Locator Strategies – Selenium Python

  • Difficulty Level : Basic
  • Last Updated : 20 Apr, 2020

Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After one has installed selenium and checked out – Navigating links using get method, one might want to play more with Selenium Python. After opening a page using selenium such as geeksforgeeks, one might want to click some buttons automatically or fill a form automatically or any such automated task. This article revolves around two strategies – Locating Single Elements and Location Multiple Elements.
Locator-Strategies-Selenium-Python

Locator Strategies to locate single first elements

Selenium Python follows different locating strategies for elements. One can locate a element in 8 different ways. Here is a list of locating strategies for Selenium in python –

LocatorsDescription
find_element_by_idThe first element with the id attribute value matching the location will be returned.
find_element_by_nameThe first element with the name attribute value matching the location will be returned.
find_element_by_xpathThe first element with the xpath syntax matching the location will be returned.
find_element_by_link_textThe first element with the link text value matching the location will be returned.
find_element_by_partial_link_textThe first element with the partial link text value matching the location will be returned.
find_element_by_tag_nameThe first element with the given tag name will be returned.
find_element_by_class_namethe first element with the matching class attribute name will be returned.
find_element_by_css_selectorThe first element with the matching CSS selector will be returned.

Locator Strategies to locate multiple elements

Selenium Python follows different locating strategies for elements. One can locate multiple elements in 8 different ways. Here is a list of locating strategies for Selenium in python –

LocatorsDescription
find_elements_by_nameAll elements with name attribute value matching the location will be returned.
find_elements_by_xpathAll elements with xpath syntax matching the location will be returned.
find_elements_by_link_textAll elements with link text value matching the location will be returned.
find_elements_by_partial_link_textAll elements with partial link text value matching the location will be returned.
find_elements_by_tag_nameAll elements with given tag name will be returned.
find_elements_by_class_nameAll elements with matching class attribute name will be returned.
find_elements_by_css_selectorAll elements with matching CSS selector will be returned.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :