Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

XPath (XML Path Language) is a query language that allows you to select nodes from an XML document. In Selenium, XPath is commonly used to locate web elements on a webpage. Here’s an in-depth look at how to use XPath in Selenium, with examples:

Basic XPath Syntax

XPath expressions are made up of location paths. These paths can be absolute or relative:

  • Absolute XPath starts from the root element and goes down the hierarchy, e.g., /html/body/div.
  • Relative XPath starts from the current node or context, denoted by a double slash //, e.g., //div.

XPath Axes

XPath includes several axes to navigate the DOM tree:

  • Self: Refers to the current node, e.g., self::node().
  • Parent: Selects the parent of the current node, e.g., parent::div.
  • Child: Selects children of the current node, e.g., child::div.
  • Descendant: Selects all descendants (children, grandchildren, etc.) of the current node, e.g., descendant::div.
  • Following-sibling: Selects all siblings after the current node, e.g., following-sibling::div.

XPath Functions

  • Text(): Selects nodes with the specified text, e.g., ‘//button[text()='Submit']‘.
  • Contains(): Selects nodes containing a specific value, e.g., ‘//button[contains(text(), 'Submit')]'.
  • Starts-with(): Selects nodes starting with a specific value, e.g., ‘//input[starts-with(@id, 'user')]‘.

Examples of XPath Expressions

1. Locating an Element by ID

				
					element = driver.find_element(By.XPATH, '//*[@id="element-id"]')

				
			

2. Locating an Element by Class Name

				
					element = driver.find_element(By.XPATH, '//*[@class="element-class"]')

				
			

3. Locating an Element by Attribute

				
					element = driver.find_element(By.XPATH, '//*[@name="element-name"]')

				
			

4. Using Text() Function

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

				
			

5. Using Contains() Function

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

				
			

6. Using Starts-with() Function

				
					element = driver.find_element(By.XPATH, '//input[starts-with(@id, "user")]')

				
			

Examples with Selenium

Here's how you can use these XPath expressions in a Selenium script:

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

# Set up the webdriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('http://example.com')

# Example 1: Locate by ID
element_by_id = driver.find_element(By.XPATH, '//*[@id="example-id"]')
print(element_by_id.text)

# Example 2: Locate by Class Name
element_by_class = driver.find_element(By.XPATH, '//*[@class="example-class"]')
print(element_by_class.text)

# Example 3: Locate by Attribute
element_by_attribute = driver.find_element(By.XPATH, '//*[@name="example-name"]')
print(element_by_attribute.text)

# Example 4: Locate by Text
element_by_text = driver.find_element(By.XPATH, '//button[text()="Submit"]')
print(element_by_text.text)

# Example 5: Locate by Contains
element_by_contains = driver.find_element(By.XPATH, '//button[contains(text(), "Submit")]')
print(element_by_contains.text)

# Example 6: Locate by Starts-with
element_by_starts_with = driver.find_element(By.XPATH, '//input[starts-with(@id, "user")]')
print(element_by_starts_with.get_attribute('id'))

# Close the browser
driver.quit()

				
			

Tips for Using XPath in Selenium

  1. Use Relative XPath: It’s generally more robust and less prone to breakage when the HTML structure changes.
  2. Combine XPath Functions: Combine multiple functions for more precise selection, e.g., //div[contains(@class, 'example') and starts-with(@id, 'user')].
  3. Optimize XPath Performance: Avoid overly complex XPath expressions as they can slow down the element search process.
Scroll to Top