← All text tools

CSS Selector to XPath Converter

Convert a CSS selector to an equivalent XPath 1.0 expression for Selenium and scraping, covering classes, ids, attributes, combinators and nth-child.

  • Free
  • No sign-up
  • Works on any device

About CSS Selector to XPath Converter

Translate a CSS selector into XPath 1.0 for Selenium, Scrapy, Puppeteer or browser consoles. Classes become whole-word tests with contains(concat(' ', normalize-space(@class), ' '), ' name '), ids and attribute selectors (=, ~=, |=, ^=, $=, *=) become attribute tests, descendant and child combinators become // and /, and the sibling combinators + and ~ use following-sibling. Position selectors such as :first-child, :last-child and :nth-child(2n+1) are written with position(), and :not() of a simple selector is supported. Selectors that need user action, like :hover, are reported as impossible.

How to use CSS Selector to XPath Converter

  1. Type or paste the CSS selector, using commas for several selectors.
  2. Choose whether the XPath starts at the whole document (//) or inside the current element (.//).
  3. Copy the XPath, or the ready Selenium and JavaScript lines that use it.

Why use this tool?

Also known as

Frequently asked questions

Why is the class test so long?

A plain @class='menu' matches only when class is exactly menu. The contains(concat(...)) form matches menu as one word among several classes, which is how a CSS class selector behaves.

What does div:nth-child(2) become?

It becomes //*[2][self::div]: the second child of its parent, but only if that child is a div. This is how CSS counts, and it differs from div[2], which means the second div.

Which CSS features have no XPath form?

Pseudo-elements such as ::before and states such as :hover or :visited, and case flags on attribute selectors. The tool names the unsupported part instead of guessing.

Is CSS Selector to XPath Converter free?

Yes. CSS Selector to XPath Converter on Toolipedia is free to use, with no sign-up and no watermark.

Is it safe to use?

Yes. Everything is worked out in your browser, and nothing you enter is sent to our server.

Does it work on my phone?

Yes. It works in any modern browser on Android and iPhone, as well as on tablets and computers. There's nothing to install.

Do I need an account?

No. Just open the page and use the tool. There's no sign-up and no email needed.

Can I put CSS Selector to XPath Converter on my own website?

Yes. Use the Embed on your site button under the tool to copy a short piece of code and paste it into your page. It is free, needs no account, and the tool keeps working inside your page.

Related tools