What is a CSS selector to XPath converter?
CSS selectors and XPath expressions are two ways to point at the same elements in an HTML page. Front-end code uses CSS (document.querySelector, stylesheets, Cypress, Playwright). Scrapers and older test tools such as Selenium, Scrapy, lxml and browser consoles often want XPath. When you already have one and need the other, this converter translates it for you, in both directions.
CSS to XPath covers tag names, #id, .class, attribute selectors (=, ~=, |=, ^=, $=, *=), the four combinators (descendant, >, +, ~), comma lists, and the pseudo-classes :first-child, :last-child, :only-child, the -of-type family, :nth-child() and :nth-of-type() with odd, even and an+b, plus :empty, :root, :checked, :disabled, :not(), :is() and :contains(). XPath to CSS handles the common subset: child and descendant steps, attribute tests, contains(), starts-with(), ends-with(), simple positions and following-sibling.
What sets it apart is the check. You can paste a sample of your own HTML and the tool runs both selectors on it. If both pick exactly the same elements, you see a green confirmation with the count. If not, it tells you the two counts. Everything runs in your browser with a parser written for this tool, and nothing you type is sent anywhere.
How to convert a selector
- Open the tool and choose the "Direction": "CSS to XPath" or "XPath to CSS". The page starts with a ready-made example.
- Type or paste your selector in the "CSS selector" field (or "XPath", depending on the direction). The result appears as you type.
- Read the result in the right-hand box, labelled "XPath" or "CSS selector", and press the copy button.
- Paste a fragment of your page into "Sample HTML to check the result on (optional)".
- Look at the line under the result. "Both pick the same elements: 1 element." means the conversion is equivalent on that HTML, and the list below shows the first matched elements.
- If a red message appears instead, read it: it says what is not supported and where the problem starts, with a character position.
- Use "continue with" to send the result to the code highlighter or the regex tester.
Examples
Class and attribute
Input in "CSS to XPath":
div.card > a[href^="https"]Output:
//div[contains(concat(' ', normalize-space(@class), ' '), ' card ')]/a[starts-with(@href, 'https')]The class test is long on purpose. A plain @class='card' would miss class="card featured", while CSS matches any element that has the card token.
Siblings and positions
| CSS | XPath |
|---|---|
h2 + p | //h2/following-sibling::*[1]/self::p |
h1 ~ p | //h1/following-sibling::p |
li:last-of-type | //li[count(following-sibling::li)=0] |
ul > li:nth-child(2n+3) | //ul/li[(count(preceding-sibling::*)+1)>=3 and ((count(preceding-sibling::*)+1)-3) mod 2=0] |
XPath back to CSS
//div[@id="app"]//span -> div#app span
//ul/li[1]/a -> ul > li:nth-of-type(1) > a
//a[contains(@href, "pdf")] -> a[href*="pdf"]Note that [@class="card"] becomes div[class="card"], an exact match on the whole attribute, not div.card. The tool keeps the meaning of what you wrote.
Something that has no equivalent
//a[text()="Home"] has no CSS version, because CSS cannot test text. The tool says so instead of guessing. In the other direction, div:hover and p::before are refused: hover depends on the mouse and pseudo-elements are not elements.
Common mistakes
- Comparing
@classwith=. In XPath@class='card'fails when the element has two classes. Use thecontains(concat(...))form that the converter produces. - Expecting
:nth-childand[n]to be the same.p:nth-child(2)means the second child, if it is ap.//p[2]means the secondpamong its siblings, which isp:nth-of-type(2)in CSS. - Using spaces or combinators inside
:not(). Only simple selectors are supported there, such as:not(.hidden). - Trying to go up. XPath can use
..orancestor::, CSS cannot. Rewrite the selector from the parent's point of view. - Using the case flag.
[type="a" i]has no XPath 1.0 equivalent and is reported as an error. - Testing on HTML that differs from the page. The check only proves equivalence on the sample, so paste markup that includes the tricky cases.
Frequently asked questions
Which XPath version does the converter use?
XPath 1.0, the version that browsers implement in document.evaluate and that Selenium, lxml and Scrapy use. Functions from XPath 2.0 and newer, such as ends-with(), are only read when converting back to CSS.
Is my HTML uploaded?
No. The conversion and the check run in your browser. The sample is parsed with DOMParser, which never executes scripts.
Why is the XPath so much longer than the CSS?
XPath has no class operator, so the whole-token check is written out with contains, concat and normalize-space. The length is the price of getting the same result as CSS.
Can I convert a list of selectors?
Yes. A comma list in CSS becomes a union with | in XPath, and the other way around.
Where can I go next?
Highlight the result for a document with the code highlighter, or test a text pattern with the regex tester.