Updated April 18, 2023
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.
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 –