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>
๐ฆ XPath - Scoped Container Search
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