How to locate elements using XPath in Selenium?
IHUB Talent: The Best Selenium with Python Training in Hyderabad with Live Internship
IHUB Talent offers the best Selenium with Python training in Hyderabad, designed to equip students with practical skills and industry-relevant knowledge. Our comprehensive program covers everything from the basics of Selenium and Python to advanced automation techniques, ensuring that students gain a solid understanding of test automation in real-world scenarios.
With a strong focus on hands-on learning, IHUB Talent provides students with opportunities to work on live projects and gain experience in automating tests for web applications. The training includes step-by-step guidance on setting up Selenium WebDriver, integrating it with Python, and using frameworks like Pay test and Unit test for efficient test management.
What sets IHUB Talent apart is our live internship program. Students not only learn from industry experts but also get the chance to apply their knowledge on live projects, making them job-ready from day one. Our trainers, who are professionals with years of experience, provide personalized mentorship to ensure that every student gets the attention they need to succeed.
In Selenium, XPath is one of the most powerful locators to find elements on a webpage. XPath (XML Path Language) lets you navigate through elements and attributes in an XML/HTML document.
Here’s a breakdown of how to locate elements using XPath in Selenium:
1. Basic Syntax
driver.find_element("xpath", "XPATH_EXPRESSION")
or (older Selenium versions)
driver.find_element_by_xpath("XPATH_EXPRESSION")
2. Types of XPath
✅ Absolute XPath
-
Starts from the root node (
/html) and follows the full path. -
Example:
driver.find_element("xpath", "/html/body/div[1]/ul/li[2]/a")⚠️ Fragile: breaks if the DOM structure changes.
✅ Relative XPath
-
Starts from anywhere in the document using
//. -
Example:
driver.find_element("xpath", "//a[@id='login']")
3. Common XPath Patterns
By attribute
//tag[@attribute='value']
//input[@id='username']
//button[@name='submit']
By text
//tag[text()='TextValue']
//button[text()='Login']
Using contains()
//tag[contains(@attribute, 'partialValue')]
//input[contains(@id, 'user')]
Using starts-with()
//tag[starts-with(@attribute, 'prefix')]
//input[starts-with(@name, 'pass')]
Using logical operators (AND / OR)
//input[@type='text' and @name='email']
//button[@id='submit' or @name='login']
Using hierarchical relationships
-
Parent → Child
//div[@class='form-group']/input -
Child → Parent
//input[@id='username']/parent::div -
Ancestor
//input[@id='username']/ancestor::form -
Following-sibling
//label[text()='Email']/following-sibling::input
4. Example in Selenium (Python)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Locate by attribute
username = driver.find_element("xpath", "//input[@id='username']")
# Locate by text
login_button = driver.find_element("xpath", "//button[text()='Login']")
# Locate by partial attribute match
password = driver.find_element("xpath", "//input[contains(@name,'pass')]")
username.send_keys("myuser")
password.send_keys("mypassword")
login_button.click()
👉 Pro tips:
-
Always prefer relative XPath for maintainability.
-
Use unique attributes (
id,name) when possible. -
Combine multiple conditions if the element is hard to identify.
Would you like me to also show you how to generate XPath automatically using browser dev tools (Chrome/Firefox)?
Read More
Comments
Post a Comment