[TOC]

Web element interactions are at the core of web automation using Selenium and Python. By leveraging the various locator strategies available, you can precisely locate and interact with web elements. Modern websites can be very complex and unique, therefore learning different locators can be a great investment.

Benefits of knowing various locator strategies:

  1. Precise Element Identification: Locators allow you to target specific elements on a web page accurately.
  2. Flexibility and Adaptability: Locators provide flexibility in locating elements by offering different strategies such as ID, class name, tag name, etc.
  3. Maintainability and Robustness: Locators help in creating maintainable and robust automation scripts.
  4. Reusability: Once you define a locator for an element, you can reuse it across multiple test cases or scenarios.
  5. Improved Readability: Locators make your automation scripts more readable and understandable.
  6. Enhanced Debugging and Troubleshooting: When an automation script encounters an issue, having well-defined locators makes debugging and troubleshooting more manageable.
  7. Cross-Browser and Platform Compatibility: Locators help ensure cross-browser and platform compatibility.
  8. Reduced Maintenance Effort: With reliable locators, your automation scripts require fewer updates and maintenance efforts.

Locator strategies

As mentioned before, Selenium comes with many locators, such as:

  • ID locator,
  • class name locator,
  • tag name locator,
  • name locator,
  • link text locator,
  • partial link text locator,
  • CSS Selector locator.

It is even possible to combine these locators with additional filters/values to make the selecting even more precise and specific for the application.

ID Locator

  • Syntax: driver.find_element(By.ID, 'element_id')
  • Description: Locates an element using its unique identifier (id attribute).

Example: To locate a login button with an ID attribute of “login-btn”, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

login_button = driver.find_element(By.ID, 'login-btn')
login_button.click()

# Closing the browser
driver.quit()

Class name Locator

  • Syntax: driver.find_element(By.CLASS_NAME, 'class_name')
  • Description: Locates an element using its class name.

Example: To locate an element with a class name of “menu-item”, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

menu_item = driver.find_element(By.CLASS_NAME, 'menu-item')

# Perform actions on the menu item

# Closing the browser
driver.quit()

Tag name Locator

  • Syntax: driver.find_element(By.TAG_NAME, 'tag_name')
  • Description: Locates an element using its HTML tag name.

Example: To locate the first paragraph element (<p>) on a webpage, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

paragraph = driver.find_element(By.TAG_NAME, 'p')

# Perform actions on the paragraph element

# Closing the browser
driver.quit()

Name Locator

  • Syntax: driver.find_element(By.NAME, 'element_name')
  • Description: Locates an element using its name attribute.

Example: To locate an input field with a name attribute of “email”, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

email_field = driver.find_element(By.NAME, 'email')
email_field.send_keys('some@email.com')

# Closing the browser
driver.quit()
  • Syntax: driver.find_element(By.LINK_TEXT, 'link_text')
  • Description: Locates a link element using the exact text of the link.

Example: To locate a link with the exact text “About Us”, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

learn_more_link = driver.find_element(By.LINK_TEXT, 'About Us')
learn_more_link.click()

# Closing the browser
driver.quit()
  • Syntax: driver.find_element(By.PARTIAL_LINK_TEXT, 'partial_link_text')
  • Description: Locates a link element using a partial match of the link text.

Example: To locate a link with partial text “Privacy Policy”, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

privacy_policy_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Privacy Policy')
privacy_policy_link.click()

# Closing the browser
driver.quit()

CSS Selector Locator

  • Syntax: driver.find_element(By.CSS_SELECTOR, 'css_selector')
  • Description: Locates an element using a CSS selector.

Example: To locate an element with the CSS selector .login-form .username-input, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

username_input = driver.find_element(By.CSS_SELECTOR, '.login-form .username-input')
username_input.send_keys('username')

# Closing the browser
driver.quit()

XPath Locator

  • Syntax: driver.find_element(By.XPATH, 'xpath_expression')
  • Description: Locates an element using an XPath expression.

Example: To locate an element with the XPath //div[@class='container']//input[@name='username'], you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

username_input = driver.find_element(By.XPATH, "//div[@class='container']//input[@name='username']")
username_input.send_keys('username')

# Closing the browser
driver.quit()