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

Mastering XPath Part 2 1738694252

This document covers advanced XPath techniques for Selenium automation, including locating elements by attributes, exact and partial text, and dynamic values. It emphasizes the importance of validating XPath syntax to avoid runtime errors and provides best practices for effective usage. The next part will explore XPath Axes for navigation.
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)
6 views12 pages

Mastering XPath Part 2 1738694252

This document covers advanced XPath techniques for Selenium automation, including locating elements by attributes, exact and partial text, and dynamic values. It emphasizes the importance of validating XPath syntax to avoid runtime errors and provides best practices for effective usage. The next part will explore XPath Axes for navigation.
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

2 Xpath
in Selenium
PART 2

Swipe for more

Kalboussi Karim
Recap from Part 1
XPath basics: absolute, relative,
and exact XPath.
Importance of mastering XPath for
Selenium automation.

In this part we will learn advanced


strategies like locating elements by
attributes, text, and more.

Swipe for more

Kalboussi Karim
Locating Elements by
Tag Name and Attribute
Syntax :
.
//tagName[@attributeName='value']

Example :
Locating a username input field:
.
//input[@id='txtUsername']
.
//input[@name='txtUsername']

Swipe for more

Kalboussi Karim
Locating Elements
with Exact Visible Text
Syntax :
.
//tagName[text()='exact text']
.
//*[text()='exact text']

Example :
Locating a hyperlink:
.
//a[text()='Pragmatic']
.
//*[text()='Pragmatic']

Swipe for more

Kalboussi Karim
Locating Elements with
Partial Visible Text
Syntax :
.
//tagName[contains(text(),'substring')]
.
//tagName[contains(.,'substring')]
.
//*[contains(text(),'substring')]

Example :
.
//a[contains(text(),'Pragmatic')]
.
//*[contains(text(),'Test')]

Swipe for more

Kalboussi Karim
Locating Elements by
Static Prefix of Visible Text
Syntax :
.
//tagName[starts-with(text(),'Prefix')]
.
//*[starts-with(text(),'Prefix')]

Example :
.
//a[starts-with(text(),'Prag')]
.
//*[starts-with(text(),'Prag')]

Swipe for more

Kalboussi Karim
Locating Input Elements
with Visible Text
Syntax :
.
//tagName[@value='visibleText']

Example :
.
//input[@value='Karim']

Tip :
Use attribute values for input elements
rather than relying on inner text.

Swipe for more

Kalboussi Karim
Locating Elements with
Multiple Attributes
Syntax :
.
//tagName[@attr1='value1'][@attr2='value2']
.
//tagName[@attr1='value1' and @attr2='value2']

Example :
.
//*[@type='submit'][@value='LOGIN']

Swipe for more

Kalboussi Karim
Locating Elements with
Dynamic Attribute Values
Syntax :
.
//tagName[contains(@attributeName,'substring')]
.
//tagName[starts-with(@attributeName,'prefix')]

Example :
.
//a[contains(@href,'pragmatic')]
.
//*[starts-with(@href,'pragmatic')]

Swipe for more

Kalboussi Karim
Validating XPath Syntax
Why Validate XPath?
Ensures correctness before execution.
Avoids runtime errors.

How to Validate:
Use browser developer tools
(Chrome/Firefox).
Plugins like XPath Helper or
SelectorsHub.

Swipe for more

Kalboussi Karim
Best Practices for
Advanced XPath Usage
1. Keep it Simple: Use the shortest path
possible.
2. Be Specific: Use unique attributes to
avoid ambiguities.
3. Validate Regularly: Test your XPath
expressions frequently during
development.
4. Combine Strategies: Mix techniques
for dynamic and static elements when
needed.

Swipe for more

Kalboussi Karim
summary
Advanced XPath techniques allow
precise, dynamic, and efficient element
location.
Combine multiple attributes, partial
matches, and prefix/suffix strategies for
flexibility.
Validate XPath expressions for accuracy
and robustness.

In Part 3, we’ll dive into:


XPath Axes: parent, child, sibling, and
ancestor navigation.

Karim Kalboussi
[email protected]

You might also like