🎉 Ends Today! Unlock up to 35% with our Black Friday lifetime discount before its gone! 🎉
In web testing, effectively selecting elements within a webpage’s Document Object Model (DOM) is crucial. CheckView supports both CSS selectors and XPath expressions for this purpose. Understanding the differences and similarities between CSS and XPath can enhance your testing strategies. Below is a guide to converting common CSS selectors into their XPath equivalents.
Description | CSS Selector | XPath Expression |
Any element | * | //* |
Any <span> element | span | //span |
Any <section> element | section | //section |
Description | CSS Selector | XPath Expression |
<input> with type equal to text | input[type=”text”] | //input[@type=”text”] |
Elements with data-role starting with admin | [data-role^=”admin”] | //*[starts-with(@data-role, ‘admin’)] |
Elements with data-id ending with 123 | [data-id$=”123″] | //*[substring(@data-id, string-length(@data-id) – string-length(‘123’) + 1) = ‘123’] |
Elements with title containing tooltip | [title*=”tooltip”] | //*[contains(@title, ‘tooltip’)] |
Description | CSS Selector | XPath Expression |
Element with ID header | #header | //*[@id=’header’] |
Description | CSS Selector | XPath Expression |
Elements with class active | .active | //*[contains(concat(‘ ‘, normalize-space(@class), ‘ ‘), ‘ active ‘)] |
Description | CSS Selector | XPath Expression |
<button> with exact text Submit | N/A | //button[text()=’Submit’] |
<p> containing text Welcome | N/A | //p[contains(text(), ‘Welcome’)] |
Description | CSS Selector | XPath Expression |
Direct child <li> of <ul> | ul > li | //ul/li |
Descendant <a> within <nav> | nav a | //nav//a |
Adjacent sibling <div> following a <header> | header + div | //header/following-sibling::div[1] |