Element Locators in Selenium WebDriver

WebDriver API Structure

WebDriver provides a comprehensive class library with two core functions:

  • Element discovery
  • Element manipulation

Installation Commands

pip install selenium==3.14.0
pip uninstall selenium
pip show selenium

Automation Testing Workflow

  1. Import required packages
  2. Instantiate browser driver
  3. Navigate to target application
  4. Locate elements
  5. Perform actions on elements
  6. Implement waits
  7. Close browser

Handling File Paths

Different approaches for specifying local file paths:

# Escaped backslashes
url = "E:\\Users\\test.html"

# Raw string notation
url = r"E:\Users\test.html"

# Forward slash format
url = "file:///E:/Users/test.html"

Note: Backslashes serve as escape characters in Python strings, so double-escaping or raw strings are necessary.

Element Location Strategies

By ID

Locates elemetns using the id attribute. Most reliable when available.

element = driver.find_element(By.ID, "submit-btn")

By Name

Targets elements via the name attribute.

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

By Class Name

Finds elements matching a specific class attribute.

element = driver.find_element(By.CLASS_NAME, "btn-primary")

By Tag Name

Selects elements by their HTML tag. Returns the first matchign element if multiple exist.

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

By Link Text

Exclusive to anchor (<a>) elements. Requires exact text matching.

element = driver.find_element(By.LINK_TEXT, "Click here for details")

By Partial Link Text

Matches anchor elements using partial text. Useful for dynamic content.

element = driver.find_element(By.PARTIAL_LINK_TEXT, "details")

Note: The find_elements_by_* methods return a list. Access elements using index notation.

elements = driver.find_elements(By.TAG_NAME, "a")
first_link = elements[0]

XPath Expressions

XPath provides flexible navigation through element hierarchy.

Absolute vs Relative Paths

# Absolute path (not recommended)
/html/body/div[2]/form/input[1]

# Relative path (preferred)
//input[@id='search']

Element Attribute Matching

//span[contains(text(),'CompanyName')]
//span[text()='CompanyName']
//div[@class="u_sp"]

Combining Attributes with Logical Operators

//div[@class="container" and @name='header']

Position-Based Selection

//book[last()]
//book[last()-1]

Advanced Attribute Functions

//*[contains(@id,'user')]
//*[starts-with(@class,'btn-')]

CSS Selectors

CSS provides concise and performant element selection.

Locator Type Syntax Example
ID #id #main-content
Class .class .btn-submit
Element element input
Attribute [attribute=value] [type='password']
Descendant ancestor descendant form input
Child parent > child ul > li

Attribute Value Substring Matching

[class^='btn']     /* starts with */
[class$='btn']     /* ends with */
[class*='btn']     /* contains */

Matching Multiple Class Values

driver.find_element_by_css_selector("input[class~='btn']")

Structural Selectors

driver.find_element_by_css_selector("div#nav > a:first-child")
driver.find_element_by_css_selector("div#nav > a:nth-child(2)")
driver.find_element_by_css_selector("div#nav > a:last-child")

# Select the last input within a span
span input:last-child

Note: The :contains() pseudo-class has been deprecated in CSS selectors.

XPath Axes

Axes enable navigation relative to the current node position.

Axis Description
ancestor All ancestor nodes (parent, grandparent, etc.)
parent Direct parenet node only
preceding All nodes appearing before current element in document
preceding-sibling Sibling nodes preceding current element
following All nodes appearing after current element in document
following-sibling Sibling nodes following current element
//div[@class='content']/ancestor::form
//input[@name='email']/following-sibling::span

Tags: Selenium webdriver automation xpath CSS selectors

Posted on Fri, 07 Aug 2026 16:55:10 +0000 by HFD