You are on page 1of 4

Xpaths Format & Examples

Serial

No. XPath Format XPath Example

1 //a Selects all <a> (anchor) elements.

Selects the element with id attribute


2 //*[@id='elementId']
"elementId".

Selects all elements with class


3 //*[contains(@class, 'highlight')]
containing "highlight".

Selects all elements with attribute


4 //*[@attribute='value']
equal to "value".

Selects the element containing the text


5 //*[text()='Login']
"Login".

Selects elements containing the partial


6 //*[contains(text(), 'user')]
text "user".

Selects the second occurrence of the


7 (xpath)[2]
element.

Selects <li> elements that are direct


8 //ul/li
children of <ul>.

9 //div/following-sibling::p Selects the element following a specific


sibling.

//*[contains(@class, 'highlight') and Selects elements with both conditions


10
text()='Login'] satisfied.

Selects all <p> elements with text content


11 //p[text()='Hello, World!']
"Hello, World!".

//*[contains(@id, 'username') and Selects elements with id attribute


12
text()='John'] "username" and text content "John".

Selects the first occurrence of the


13 (xpath)[1]
element.

Selects elements with data-custom


14 //*[contains(@data-custom)]
attribute.

Selects elements with class starting with


15 //*[starts-with(@class, 'btn')]
"btn".

//*[data-type='button' and Selects elements with both data-type


16
@role='submit'] and role attributes specified.

Selects elements with a "data-testid"


17 //*[@data-testid]
attribute.

Selects elements without a "disabled"


18 //*[@!disabled]
attribute.

19 //*[text()=''] Selects elements with empty text content.


Selects child elements based on their
20 //div[@data-section]/child::*
parent's attribute.

Selects elements based on the existence


21 //*[exists(@data-custom)]
of a specific attribute.

Selects elements with type attribute set


22 //*[@type='checkbox']
to "checkbox".

Selects elements with class attribute


23 //*[contains(@class, 'btn')]
containing "btn".

24 //*[starts-with(@data-)] Selects elements with data- attribute.

//*[@title[string-length(@title) = Selects elements with title attribute of


25
10]] length 10.

Selects elements with a specific parent

26 //ul/li (e.g., selects all <li> elements within a

<ul>).

Selects elements with a specific ancestor

27 //div//span (e.g., selects all <span> elements under a

<div>).

Selects elements based on child count


28 //div[count(p) = 3]
(e.g., selects all <div> elements with
exactly three child <p> elements).

Selects elements in a list based on their

29 //ol/li[2] position (e.g., selects the second <li> in

an ordered list).

Selects elements based on their

30 //h1/following-sibling::* relationship with a sibling (e.g., selects the

next sibling of an <h1> element).

You might also like