Docsity

XPath Cheat Sheet, Cheat Sheet of Web Programming and Technologies

Partial preview of the text

Download XPath Cheat Sheet and more Cheat Sheet Web Programming and Technologies in PDF only on Docsity!

XPath

Cheat Sheet

HTML web page

XPath and CSS Selectors

XPath expressions simplified

I'm paragraph within a div with a class set to intro I'm a span with ID set to location and i'm within a paragraph

I'm a paragraph with ID set to outside and i'm within a div with a class set to intro

Hi i'm placed immediately after a div with a class set to intro

Div with a class attribute set to intro

Item 1 Item 2 Item 3 Item 4

Google Google France

Hi, I have two classes

Hi i'm bold

BASICS

An element is a tag in the HTML markup. Example: The ‘p’ tag aka paragragh is called an element. To select any element from HTML web pages we simply use the following syntax Example: To select all p elements we can use the following XPath selector Although this approach works perfectly fine, it’s not recommended to use it, because if for example we want only to select the “ p ” elements that are inside the first div with a class attribute equals to “ intro ” this approach won’t be the best solution, this is why we always prefer to target elements either by their class attribute, id or by position so we can limit the scope of the XPath expression. //p

OR:

Although the element does have two classes we can for example search for a substring within the class attribute value by using the contains function. REMEBER: //p[contains(@class, ‘italic’)] The contains function takes two arguments:The first one is where to search, whether on the class attribute value, id or anything else.The second argument is the value you’re looking for.The value you search for is also case sensitive, so be careful!

Value lookup

Let’s say you want to select all the “ a ” elements in which the “ href ” attribute value starts with “ https ” and not “ http ”, in this case we can use the following XPath expression: So search for the text at the beginning we use the caret sign “ starts- with ” function which takes the same arguments as the contains function. Now if you want to search for a value at the end we use the “ ends- with ” function, however, this function is not supported on XPath version 1.0 which is the version used by the majority of the browsers and LXML. Finally if we want to search for a particular value in between we use the contains function as explained before. If you want to get the text of a particular element you can use the text function, for example, to get the text element of the “ p ” element with id equals to “ outside ” we use the following XPath expression: //a[starts-with(@class, ‘https’)] //p[@id=”outside”]/text()

XPath axes

In XPath an axis is used to search for an element based on its relatioship with another element, we have some axes which we can use to navigate up and down in the HTML markup. All axes in XPath use the follwing syntax:

XPath axes (GOING UP)

 The parent

o The parent axis is used to get the parent of a specific

element, for example the get the parent of the “ p ” element with id equals to “outside” we use the following XPath expression:

o The node() function in XPath is used to get the “element”

no matter what its type is.

 The ancestor

o The ancestor axis can be used to all the ancestors of a specific element, for example to get the ancestors( parent , grand parent , ...) of the “ p ” element with id equals to “ outside “ we use the following XPath expression:

 The preceding

o In XPath the preceding axis will get all the elements that do precede an element excluding its ancestors. //p[@id="outside"]/parent::node() //p[@id="outside"]/ancestor::node() ElementName::axis

 Preceding sibling

o In XPath the preceding-sibling axis will get the sibling that do precede an element, in other words it will return the brother that is on the top of a specific element.

XPath axes (GOING DOWN)

To go down on the HTML markup we also have 4 axis which are: o The child axis which will get the children of a specific element o The following axis which will return all the elements that are after the closing tag of a specific element. o The following-sibling axis which will return all the elements that are after the closing tag of an element but these elements should share the same parent. o The descendant axis which will get the descendants of a particular element.