Selenium Locating Strategies (original) (raw)

Last Updated : 9 Jun, 2026

Selenium locating strategies are methods used to identify web elements (like buttons, input fields, and links) on a webpage. These locators help automation scripts interact with elements accurately and reliably. Choosing the right locator improves test stability and reduces script failures.

Types of Locators in Selenium

Selenium provides different types of locators to identify web elements on a webpage. These locators help automation scripts interact with elements like buttons, input fields, links, and more.

1. Locating By ID

The ID attribute in HTML is a unique identifier assigned to a web element. Since it is unique on a webpage, it is one of the most reliable and fastest ways to locate elements in Selenium.

Locating elements by ID is faster than methods like XPath or CSS Selector because it uses browser-level optimization.

In Selenium, we use the By.ID locator provided by the By class to locate elements using their ID.

**Syntax:

element = driver.find_element(By.ID, "element_id")

Python `

Python program to implement Locating by ID

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/"
driver.get(url)

Replace ID_NAME with the Id of your element

try:

# Replace this with the id of the element
element = driver.find_element(By.ID, "nav_tab_courses") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by ID

Selenium Locating Strategies

**Explanation:

2. Locating By Class Name

The class attribute in HTML is used to group multiple elements that share similar styling or functionality. Since many elements can share the same class name, it is useful for locating elements when uniqueness is not required.

However, class names are not always unique, so this locator may return multiple elements. That’s why it is important to use it carefully.

In Selenium, we use the By.CLASS_NAME locator provided by the By class to locate elements using their class attribute.

**Syntax:

element = driver.find_element(By.CLASS_NAME, "element_class_name")

Below is the Python program to implement Locating by Class Name:

Python `

Python program to implement Locating by Class Name

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/"
driver.get(url)

Replace ID_NAME with the Id of your element

try:

# Replace this with the id of the element
element = driver.find_element(By.CLASS_NAME, "entry-title") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by Class Name

Selenium Locating Strategies

**Explanation:

3. Locating By Name

The name attribute in HTML is used to identify form elements such as input fields, radio buttons, and checkboxes. Unlike the id attribute, the name value is not always unique on a webpage.

It is mainly used in HTML forms to collect and submit user input data. Because of this, the name locator is especially useful when working with form elements in Selenium.

In Selenium, we use the By.NAME locator provided by the By class to locate elements using their name attribute.

**Syntax:

element = driver.find_element(By.NAME, "element_name")

**Here,

**driver: This is the selenium web driver instance.

**find_element(): This method is used to locate element in Selenium.

**By.NAME: This specifies that you're locating the element by its name.

**element_name: Replace this with the actual name of the element you want to locate.

Below is the Python program to implement Locating by name:

Python `

Python program to implement Locating by name

import time

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/about/contact-us/"
driver.get(url)

Replace ID_NAME with the Id of your element

try:

# Replace this with the name of the element
element = driver.find_element(By.NAME, "feedback_area") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by Name

Selenium Locating Strategies

**Explanation:

4. Locating By Tag Name

Tag Name locator is used to identify web elements using their HTML tag such as input, button, div, etc. It is useful when working with multiple elements of the same type on a webpage. However, it is not suitable for uniquely identifying elements.

**Syntax:

element = driver.find_element(By.TAG_NAME, "tag_name")

Below is the Python program to implement Locating by tag name:

Python `

Python program to implement Locating by tag name

import time

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/about/contact-us/"
driver.get(url)

try:

# Replace this with the Tag Name of the element
element = driver.find_element(By.TAG_NAME, "select") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by Tag Name

Selenium Locating Strategies

**Explanation:

5. Locating by CSS Selectors

CSS Selectors are patterns used to locate HTML elements based on tag name, class, id, attributes, or hierarchy. They are widely used in Selenium because they are fast, flexible, and powerful for identifying web elements.

**Common CSS Selectors

Syntax:

driver.find_element(By.CSS_SELECTOR, "element_css_selector")

Below is the Python program to implement Locating by CSS Selector:

Python `

Python program to implement Locating by CSS Selector

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/about/contact-us/"
driver.get(url)

try:

# Replace this with the css selector of the element
element = driver.find_element(By.CSS_SELECTOR, "div.title") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by CSS Selector

Selenium Locating Strategies

**Explanation:

6. Locating By XPath

XPath (XML Path Language) is a powerful locator strategy used to identify web elements on a webpage based on attributes, hierarchy, and relationships in the HTML structure. It is widely used in Selenium when other locators like ID, class, or name are not available.

**Syntax:

element = driver.find_element(By.XPATH, "element_xpath")

Below is the Python program of Locating by XPath:

Python `

Python program of Locating by XPath

from selenium import webdriver from selenium.common import NoSuchElementException from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your actual url

url = "https://www.geeksforgeeks.org/about/contact-us/"
driver.get(url)

try:

# Replace this with the XPATH selector of the element
element = driver.find_element(By.XPATH, 
                              "/html/body/div[3]/div[2]/div[1]/div/div") 

except NoSuchElementException: print("Not Found") else: print("Found")

`

**Output:

Locating by XPath

Selenium Locating Strategies

**Explanation:

Link Texts as the name suggests are primarily used to locate anchor tags '' elements on a webpage . Anchor tags are primarily used for hyperlinks (those which navigate us to different page or resource on a website).

**Syntax:

Link Text: element = driver.find_element(By.LINK_TEXT, "element_link_text")

Partial Link Text: element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")

Below is the Python program to implement Locating using Link Texts and Partial Link Texts:

Python `

Python program to implement Locating

using Link Texts and Partial Link Texts

from selenium import webdriver from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

Replace this with your url

url = "https://www.example.com/" driver.get(url)

Replace Learn more with the Link Text of the element

element1 = driver.find_element(By.LINK_TEXT, "Learn more");

Replace Click with the Link Text of the element

element2 = driver.find_element(By.PARTIAL_LINK_TEXT, "Click");

`

**Output:

Locating using Link Texts and Partial Link Texts

Selenium Locating Strategies

**Explanation:

CSS Selector Vs XPath in Selenium

CSS Selector and XPath are both used to locate web elements in Selenium, but they differ in syntax, performance, and capabilities. CSS Selectors are generally faster and simpler, while XPath is more powerful and flexible for complex element selection.

Feature CSS Selector XPath
Speed Faster Slower compared to CSS
Syntax Simpler and cleaner More complex but powerful
Direction Only forward (parent -> child) Both forward and backward
Text Handling Cannot directly locate by text Can locate using text()
Traversal Limited navigation Full DOM traversal support
Usage Preferred when possible Used when CSS is not sufficient