Complete XPath Cheatsheet

Cheatsheet Web

Complete generic XPath reference with real-world patterns and examples for locating elements in HTML and XML

What is this? Official Docs

XPath is a query language used to locate and navigate elements in HTML and XML documents.

What is XPath?

XPath (XML Path Language) is a query language used to locate and navigate elements within HTML and XML documents. It allows precise element selection using tags, attributes, text, and DOM relationships.

โšก Key Features

  • DOM traversal
  • Attribute and text matching
  • Axis-based navigation
  • Tool and language agnostic

๐ŸŽฏ Use Cases

  • UI automation
  • Web scraping
  • DOM inspection
  • Dynamic element identification

๐Ÿ“ XPath - Absolute Path

Select element using full DOM hierarchy.

XPath:

/html/body/div/form/input

Example (HTML):

<form>
  <input />
</form>

Avoid in real projects due to fragility.

๐ŸŽฏ XPath - Relative Path

Select element anywhere in the document.

XPath:

//input

Example (HTML):

<input type="text" />

Recommended for maintainable locators.

๐Ÿท๏ธ XPath - Select by Tag Name

Select elements by HTML tag.

XPath:

//button

Example (HTML):

<button>Save</button>

๐Ÿ” XPath - Select by Attribute

Match exact attribute value.

XPath:

//input[@id='username']

Example (HTML):

<input id="username" />

๐Ÿ”— XPath - Multiple Attributes

Match multiple attribute conditions.

XPath:

//input[@type='text' and @name='email']

Example (HTML):

<input type="text" name="email" />

๐Ÿ”€ XPath - OR Condition

Match any one condition.

XPath:

//input[@id='email' or @name='email']

Example (HTML):

<input name="email" />

๐Ÿ“Œ XPath - contains() Attribute

Partial attribute match.

XPath:

//div[contains(@class,'header')]

Example (HTML):

<div class="main-header"></div>

โ–ถ๏ธ XPath - starts-with()

Match attribute prefix.

XPath:

//input[starts-with(@id,'user_')]

Example (HTML):

<input id="user_name" />

โœ๏ธ XPath - Exact Text Match

Match exact visible text.

XPath:

//button[text()='Login']

Example (HTML):

<button>Login</button>

๐Ÿ“ XPath - contains(text())

Match partial visible text.

XPath:

//span[contains(text(),'Welcome')]

Example (HTML):

<span>Welcome John</span>

๐Ÿงน XPath - normalize-space()

Ignore extra whitespace.

XPath:

//button[normalize-space()='Submit']

Example (HTML):

<button>  Submit </button>

๐Ÿ”ข XPath - Index Selection

Select element by position (1-based).

XPath:

(//input[@type='text'])[1]

Example (HTML):

<input type="text" />
<input type="text" />

Use cautiously โ€” indexes may change.

๐Ÿ”š XPath - last() Function

Select last matching element.

XPath:

(//button)[last()]

Example (HTML):

<button>Cancel</button>
<button>Save</button>

โฌ†๏ธ XPath - Parent Axis

Navigate to parent element.

XPath:

//input[@id='email']/parent::div

Example (HTML):

<div>
  <input id="email" />
</div>

โฌ‡๏ธ XPath - Child Axis

Select direct children.

XPath:

//ul/child::li

Example (HTML):

<ul>
  <li>Item</li>
</ul>

๐ŸŒณ XPath - Ancestor Axis

Navigate upward in DOM.

XPath:

//input[@id='email']/ancestor::form

Example (HTML):

<form>
  <div>
    <input id="email" />
  </div>
</form>

๐ŸŒฟ XPath - Descendant Axis

Select all nested children.

XPath:

//form//descendant::input

Example (HTML):

<form>
  <div>
    <input />
  </div>
</form>

โžก๏ธ XPath - Following Sibling

Select next sibling.

XPath:

//label[text()='Username']/following-sibling::input

Example (HTML):

<label>Username</label>
<input />

โฌ…๏ธ XPath - Preceding Sibling

Select previous sibling.

XPath:

//input[@id='password']/preceding-sibling::label

Example (HTML):

<label>Password</label>
<input id="password" />

๐Ÿ“Š XPath - Table Row Match

Select row by cell value.

XPath:

//tr[td[text()='John']]

Example (HTML):

<tr>
  <td>John</td>
</tr>

๐ŸŽฌ XPath - Action Inside Table Row

Select element inside matched row.

XPath:

//tr[td[text()='John']]//button

Example (HTML):

<tr>
  <td>John</td>
  <td><button>Edit</button></td>
</tr>

โ˜‘๏ธ XPath - Checkbox Near Label

Locate input associated with label.

XPath:

//label[text()='Accept']/preceding-sibling::input

Example (HTML):

<input type="checkbox" />
<label>Accept</label>

Search within a container.

XPath:

//div[@id='modal']//button[text()='Save']

Example (HTML):

<div id="modal">
  <button>Save</button>
</div>

๐ŸŽจ XPath - SVG Elements

Handle SVG elements using name().

XPath:

//*[name()='svg']//*[name()='path']

Example (HTML):

<svg>
  <path />
</svg>

๐Ÿž XPath - Debug XPath

Test XPath in browser console.

$x("//input[@id='username']")

Always validate XPath before using it in automation or scraping.

โญ XPath - Best Practices

Recommended rules:

  • Prefer relative XPath
  • Avoid absolute paths
  • Use stable attributes
  • Avoid indexes when possible
  • Keep XPath short and readable