EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XPath cheat sheet
Secondary Sidebar
Asp.Net MVC Interview Questions

DB2 Interview Questions

What is ARP?

Switch Statement in JavaScript

Remove Duplicate Elements from Array Javascript

Sort Array 0s,1s and 2s

XPath cheat sheet

XPath cheat sheet

Definition of XPath cheat sheet

Xpath Cheat Sheet is defined as a composed list or a summary of Xpath methods. When working with site scrapers or data integration, saves us time. Finding a unique address/path for a web element is difficult because there may be similar tags, same attribute values, and identical paths, making it difficult to produce an accurate unique URL for a web element called “XPATH.” When running Selenium automation testing, we can utilize the cheat sheet as a reference.

In this article, we will discuss what Xpath is and how it helps the user to have a glimpse of the tools and their elements to grab it easier.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

XPath cheat sheet

Finding the appropriate elements and sections in Xpath is a key part of building sustainable and robust web automation. As a result, I’ve put together the most comprehensive XPath cheat sheet available. The cheat sheet included below comprises the majority of functions required by SEOs, and should work well in all sorts of IM scraping. It also includes various functions and expressions that can be quite valuable for experienced professionals.

Important Functions and Tags of Xpath are listed below:

Initially, we shall go with Xpath Expressions as they are the pattern for selecting a set of nodes from an XML document. Xpath Expressions has the following prefix.

Basic Expression

//* It Selects all the nodes
/ Select the next immediate element in the list. Establishes the biggest node in the document.
// Selects anywhere or pulls all the elements in the document.
/.. Gives a parent or root of
:: Scope Level
//text() Extracts all the text from a web page.

 

Expression Example Description
// //p[@class=‘down’] Choose a paragraph tag from any location in the DOM.
/ /a All anchor (a) tags from the provided node should be found.
//a/Text() Specifies texts of all the anchor tags.
//a/@href This gives href value from an element.
/ Books/Book name/Page No Find all Page No Starting from the root node.

Using dot (.)

1. .// Retrieves all tags below the present node
2. ./p  Gives all direct <p> children

Getting the contents

//blog/text() Nodes are returned by all of those expressions. When we need to get to a node’s content quickly, utilize text ()

Tags

Using contains:

The Syntax goes like this:

//tag[contains(attribute,'value')]

The Login link goes like this

//div[@id='bar']//a[contains(text(),'login name')]

1 // e Extracts elements in the document by reference
2 (//e)[2] Specifies the next element on the page
3 //img  Extracts an element with an image
4 //e[@attribute] Extracts an element with the respective attribute.
5 //e[contains(@Att,”text”)] element <e> with the text ‘t’ in the attribute A
6 //e[starts-with(@Att, “text”)] element <e>, whose attribute A starts with the letter ‘t’
7 //e[ends-with(@Att, “text”)] element <e>, whose attribute A ends with the letter ‘t’

Table Cell

//*[@id=”demo”]//tr[2]//td[1]  Gives a table with second row and first column
//td[preceding-sibling::td=”t”] Gives a cell immediately having a Sibling
td[preceding-sibling::td[contains(.,”t”)]] Gives a cell immediately having a Sibling

Arithmetic Functions

Performs Arithmetic Calculations

Sum(//interest/@year) Total interest of a year
Ceiling((sum(//interest/@year)/count(//interest))) Average interest is rounded.
Min(//interest/@year)  Minimum value is calculated
Max(//interest/@year) Maximum value is calculated

Conversions

Boolean(expr)  It returns true or false results
String(obj val)  Makes a String Conversion
Number(obj val)  Does Conversion of an object to a number

String Functions

1) starts-with(string1, string2) When the first string begins with the second string, it returns true.
2) contains(string1, string2) When the first string contains the second string, it returns true.
3) substring(string, offset, length) It gives a chunk of the string to work with. The segment begins at an angle and continues for the length specified.
4) substring-before(string1, string2) It returns the portion of string1 that occurs up until the first occurrence of string2.
5) substring-after(string1, string2) After the first occurrence of string2, it returns the section of string1 that follows.
6) string-length(string) It gives the length of a string in characters.

Example of before and after sub-string

substring-before("11/12", "/")

The output of before: 11

The output of after: 12

Xpath Axes

1 Child::span Selects all the children under the span
2 Descendant::span Selects the descendant part of the span
3 Ancestor:: Selects the current node  under the span
4 Following::span Specifies the following node
5 Following-sibling::span Selects the sibling of a node
6 Preceding::span Selects preceding nod types
7 Preceding-sibling::span Selects preceding sibling type

Xpath for YouTube Link

1 //h3/a The title of the doc is extracted
2 //h3/a/@href URL Link is extracted
3 (//*[contains(@class,‘yt-user-info’)])[1] User Information is retrieved from a link.
4 //*[(@class=‘watch-time-text’)] Extracts a text from a watch list

Selectors: Order List

ul > li:first-of-type //ul/li[1] Select first li tag preceding a child of ul
ul > li:nth-of-type(2) //ul/li[2] Select second li tag preceding a child of ul
li#id:first-of-type //li[1][@id=”id”] Select the first li with id value “id”
ul > li:last-of-type //ul/li[last()] Select the last li also a child of ul
a:first-child //*[2][name()=”a”] Select second child of the anchor tag

Wildcards Nodes

* This matches any HTML element
@* This matches any attribute of an element
node() This matches any type of a node

Follow the steps below to practice the aforementioned XPath:

The following is a list of prerequisites.

Step 1: To examine XPath, install the Chropath extension on the Chrome browser and restart Chrome.

Note:

In Selenium, XPath is one of the most extensively used web locators. Creating an effective XPath, on the other hand, is a skill that is heavily influenced by the document’s structure and the WebElement’s current position. Finding the closest unique WebElement relative to the target element and utilizing the best technique to locate the WebElement is one of the most important things to do.

Conclusion

Therefore User will become a master of developing your XPath if we master all of these techniques, and will be able to build killer XPaths with extremely few chances of becoming invalid. Finally, we’d like to emphasize that requesting that your development team add unique ids to all site items that you’re interested in will greatly simplify your life. The greater your understanding of the code, the more options we shall have for constructing successful XPATHs.

Recommended Articles

This is a guide to XPath cheat sheet. Here we discuss the definition, Example of before and after sub-string respectively. You may also have a look at the following articles to learn more –

  1. XPath Relative
  2. XPath Axes
  3. XPath Nodes
  4. XPath Expressions
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more