Mastering XPath in Selenium with Python

๐Ÿ” What is XPath?

XPath (XML Path Language) is a query language used to navigate through elements and attributes in an XML or HTML document. Selenium uses XPath to locate elements on a web page when other selectors like ID or class are not enough.


✅ Basic Setup with Selenium and Python

bash

Copy

Edit

pip install selenium

python

Copy

Edit

from selenium import webdriver


driver = webdriver.Chrome()

driver.get("https://example.com")


# Example using XPath

element = driver.find_element("xpath", "//h1")

print(element.text)


driver.quit()

๐Ÿ“˜ Types of XPath

1. Absolute XPath

Navigates from the root node


Fragile and not recommended for dynamic content


xpath

Copy

Edit

/html/body/div[1]/section/h1

2. Relative XPath ✅ (Recommended)

Starts from a known element and is more stable


xpath

Copy

Edit

//div[@class='product']//h2

๐Ÿ”ง XPath Syntax Essentials

๐Ÿ”น Attributes

xpath

Copy

Edit

//input[@name='email']

๐Ÿ”น Text Matching

xpath

Copy

Edit

//button[text()='Submit']

๐Ÿ”น Contains (for partial matches)

xpath

Copy

Edit

//div[contains(@class, 'card')]

๐Ÿ”น Starts-with

xpath

Copy

Edit

//input[starts-with(@id, 'user')]

๐Ÿ”น Logical Operators

xpath

Copy

Edit

//input[@type='text' and @name='username']

๐Ÿงฉ Navigating the DOM

๐Ÿ”ธ Parent Node

xpath

Copy

Edit

//span[@class='price']/..

๐Ÿ”ธ Child Nodes

xpath

Copy

Edit

//div[@class='menu']/ul/li

๐Ÿ”ธ Following/preceding siblings

xpath

Copy

Edit

//label[text()='Email']/following-sibling::input

๐Ÿ›  Using XPath in Selenium Python

python

Copy

Edit

from selenium.webdriver.common.by import By


# Find element with XPath

element = driver.find_element(By.XPATH, "//button[contains(text(), 'Login')]")

element.click()

✅ You can also use find_elements(By.XPATH, ...) to get a list.


๐Ÿงช XPath Tips for Robustness

Prefer relative XPath


Avoid long, deeply nested paths


Use unique attributes, contains(), or indexed selections for dynamic pages


Use browser DevTools: right-click → Inspect → Copy XPath (but tweak it afterward)


๐Ÿ” Example: Dynamic Table Row Selection

xpath

Copy

Edit

//table[@id='orders']//tr[td[text()='Order #12345']]/td[3]

This finds the 3rd column of the row where the first column contains "Order #12345".


๐Ÿš€ Advanced Use Cases

Click a button next to a specific label:

xpath

Copy

Edit

//label[text()='Username']/following::input[1]

Select the last item in a list:

xpath

Copy

Edit

//ul[@id='items']/li[last()]

๐Ÿ“š Resources for Practice

XPath Tester


Try XPath expressions directly in Chrome DevTools:


js

Copy

Edit

$x("//h1[contains(text(), 'Welcome')]") 

Learn Selenium Python Training in Hyderabad

Read More

What are Locators in Selenium and How to Use Them

๐Ÿ” Locating and Interacting with Web Elements

Setting Up Selenium in PyCharm or VS Code

Top Tools You Need to Start Selenium with Python

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions 

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Tosca for API Testing: A Step-by-Step Tutorial

Working with Tosca Parameters (Buffer, Dynamic Expressions)