This project includes a comprehensive HTML page to test and compare the performance of XPath and CSS selectors across various scenarios. The HTML page contains examples where XPath and CSS selectors are tested to understand their relative performance.
-
Sibling Element Selection
- XPath Selector:
//h2/following-sibling::p[1] - CSS Selector:
h2 + p
- XPath Selector:
-
Attribute Value Containment
- XPath Selector:
//button[contains(@data-action, 'submit')] - CSS Selector:
button[data-action*='submit']
- XPath Selector:
-
Last Element in a List
- XPath Selector:
//ul[@class='items']/li[last()] - CSS Selector:
.items li:last-child
- XPath Selector:
-
Complex Conditional Logic
- XPath Selector:
//div[@data-status='active' and @data-role='user'] - CSS Selector:
.box[data-status='active'][data-role='user']
- XPath Selector:
-
Partial Attribute Value
- XPath Selector:
//input[starts-with(@name, 'user')] - CSS Selector:
input[name^='user']
- XPath Selector:
-
Simple Element Selection
- XPath Selector:
//div[@class='box'] - CSS Selector:
.box
- XPath Selector:
-
Select Hidden Elements
- XPath Selector:
//div[@class='hidden'] - CSS Selector:
.hidden
- XPath Selector:
-
Descendant Selection
- XPath Selector:
//div[@class='content']//p - CSS Selector:
.content p
- XPath Selector:
-
Open the HTML File
- Open
perf.htmlin a web browser.
- Open
-
Run JavaScript Performance Tests
- The performance tests will run automatically when the page loads.
- The results will be logged to the browser’s developer console.
-
View Performance Results
- Open the developer console (
F12orCtrl+Shift+C/Cmd+Option+Ion Mac). - Check the console logs for timing results of each XPath and CSS selector test.
- Open the developer console (
- XPath may provide advantages in complex scenarios, such as conditional logic or sibling element selection.
- CSS selectors are typically faster and more efficient for straightforward queries and element selection.
- Performance can vary depending on the browser’s implementation and the complexity of the DOM.