0% found this document useful (0 votes)
21 views12 pages

Mastering XPath

XPath is a query language used to locate nodes in XML documents, essential for element location in Selenium during automated testing. There are three types of XPath: Absolute, Relative, and Exact, each with distinct characteristics and use cases. This document serves as an introduction to XPath fundamentals in Selenium, with future content promising to cover advanced techniques.

Uploaded by

Daura Rady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Mastering XPath

XPath is a query language used to locate nodes in XML documents, essential for element location in Selenium during automated testing. There are three types of XPath: Absolute, Relative, and Exact, each with distinct characteristics and use cases. This document serves as an introduction to XPath fundamentals in Selenium, with future content promising to cover advanced techniques.

Uploaded by

Daura Rady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Mastering

Xpath
in Selenium
PART 1

Swipe for more

Kalboussi Karim
What is XPath?
XPath (XML Path Language) is a
query language used to locate
nodes in an XML document.

In Selenium, XPath helps locate


elements in HTML documents
(web pages) during automated
testing.
Xpath

Swipe for more

Kalboussi Karim
Types of XPath
Absolute XPath
Relative XPath
Exact XPath

XPath

Absolute Relative Exact


XPath XPath XPath

Swipe for more

Kalboussi Karim
Xpath Importance
XPath enables precise element
location even in complex DOM
structures.
It supports dynamic content
handling, which is common in
modern web applications.
XPath provides flexible options
for element selection
using attributes,
hierarchy, and more.

Swipe for more

Kalboussi Karim
What is Absolute XPath?
-Absolute XPath specifies the full path
from the root node to the target element.
-Key Characteristics:
Starts with a single /.
Breaks easily with DOM changes.

-Syntax Example:
/html/body/div/header/nav/ul/li[2]

Swipe for more

Kalboussi Karim
What is Relative XPath?
-Relative XPath starts from a specific
node in the DOM, not the root node.
-Key Characteristics:
Starts with //.
More robust to DOM changes.

-Syntax Example:
//nav/ul/li[@class='menu-item'][3]

Swipe for more

Kalboussi Karim
What is Exact XPath?
-Exact XPath refers to techniques or
conditions ensuring precise identification
of elements.
-Key Characteristics:
Utilizes specific attributes (id, class, etc.).
Combines with logical operators.
Ideal for identifying dynamic elements.
-Syntax Example:
//button[contains(@class, 'submit') and @type='submit']

Swipe for more

Kalboussi Karim
Absolute vs Relative XPath
Aspect Absolute XPath Relative XPath

Starts with / //

Starts from a node


Structure full path from the root
anywhere in DOM

More flexible, adapts


Reliability Breaks if DOM changes
to changes

Easy to understand but Shorter, can include


Complexity
long conditions

Swipe for more

Kalboussi Karim
Examples of XPath Usage
1. Absolute XPath Example:
/html/body/div/main/section/article[1]/h1
Locates the first <h1> tag in the first <article>
in the section.

2. Relative XPath Example:


//input[@name='username']
Finds an input field with the name attribute
set to "username".

3. Exact XPath Example:


//button[contains(@class, 'reset') and @type='reset']
Finds a button with a "reset" class and a type
attribute of "reset".

Swipe for more

Kalboussi Karim
When to Use Absolute XPath?
Use Absolute XPath:
When working with simple, static pages.
For quick tests where DOM changes are
unlikely.

Avoid Absolute XPath:


When dealing with dynamic or frequently
changing content.

Swipe for more

Kalboussi Karim
When to Use Relative or
Exact XPath?
Use Relative XPath:
For complex, dynamic pages.
When flexibility and robustness are
critical.

Use Exact XPath:


To pinpoint specific, unique elements.
When working with dynamic attributes or
conditional selections.

Swipe for more

Kalboussi Karim
summary
XPath is a powerful tool for locating
elements in Selenium.
Absolute XPath provides full paths but
lacks flexibility.
Relative and Exact XPaths offer dynamic
and precise solutions.

This is just the beginning! Part 1 introduced


the fundamentals of XPath in Selenium. In the
upcoming posts, we’ll explore more about
Advanced XPath techniques.

Karim Kalboussi
[email protected]

You might also like