# XPath Tester and CSS Selector Tester Online > Source: https://scrape.do/tools/xpath-css-tester/ Free XPath and CSS selector tester. Paste HTML or XML, see every match highlighted with its text and attributes, and copy it as Python. Nothing is uploaded. Paste HTML or XML, type an XPath expression or a CSS selector, and see every match highlighted in the page with its text, attributes and outer HTML. The count updates as you type, broken expressions get a plain-English error, and any selector copies as a Python Scrape.do call. It runs in your browser, so nothing you paste is uploaded. ## How to use the XPath tester 1. **Paste the HTML or XML.** An example product page is already loaded. To test a real page, view its source (Ctrl+U on Windows and Linux, Cmd+Option+U on a Mac), copy it and paste it, or open a saved `.html` or `.xml` file. 2. **Pick XPath or CSS and type.** The match count updates as you type. One click on an example fills in a working selector. 3. **Read the matches.** Each match shows its text, its attributes, its outer HTML and its absolute path. **Highlighted** shows where the matches sit in the parsed page. 4. **Copy it as code.** The snippet under the tool runs the same selector in Python on the live page. Typed CSS in XPath mode, or the other way round? The tester notices and switches. ## What are XPath and CSS selectors? **Both are query languages that point at parts of an HTML or XML document. A CSS selector like `article .price` matches elements by tag, class, id and attributes, the way a stylesheet does. An XPath expression like `//article//span[@class="price"]/text()` walks the document tree in any direction, matches on text, and can return text nodes, attribute values and numbers as well as elements.** For scraping, that difference decides which one you reach for: - **CSS is shorter** and reads like the page's own styles. Most simple jobs (every price, every product link) fit in one short selector. - **XPath does what CSS can't.** It finds an element by its text, steps to a parent or a following sibling, and counts or joins values in the expression itself. parsel (the selector library inside Scrapy) adds `::text` and `::attr(name)` to CSS, so in Python you can get text and attribute values without XPath. ## XPath and CSS selector cheat sheet The same lookups side by side. The CSS column uses parsel's `::text` and `::attr()` where plain CSS has no way to return a value. | What you want | XPath | CSS | |---|---|---| | Every link | `//a` | `a` | | Element by id | `//*[@id="main"]` | `#main` | | Element with a class | `//div[contains(@class, "price")]` | `div.price` | | Link URLs | `//a/@href` | `a::attr(href)` | | Text of a heading | `//h1/text()` | `h1::text` | | Direct children | `//ul/li` | `ul > li` | | Any descendant | `//div//a` | `div a` | | Second item in each list | `//ul/li[2]` | `ul > li:nth-of-type(2)` | | Attribute starts with | `//a[starts-with(@href, "https")]` | `a[href^="https"]` | | Attribute contains | `//a[contains(@href, "/product/")]` | `a[href*="/product/"]` | | Element by its text | `//a[contains(text(), "Next")]` | not in standard CSS | | Parent of a match | `//span[@class="price"]/..` | not in standard CSS (`:has()` selects any ancestor, not the direct parent) | | Cell after a label | `//th[text()="Price"]/following-sibling::td` | `th + td`, but not by the label's text | | Count matches | `count(//li)` | count them in code | Two traps hide in that table. `@class="price"` must match the whole attribute, so it misses `class="price sale"`. Use `contains(@class, "price")` (which also matches `price-old`) or parsel's `has-class("price")`. And `//ul/li[2]` means the second `li` of each list, while `(//ul/li)[2]` means the second one on the whole page. ## Common scraping patterns These cover most of the selectors a scraper needs. Each one runs on the example page or the sitemap example in the tool. - **A value next to its label.** Spec tables and definition lists put the label in one cell and the value in the next: `//th[text()="Layout"]/following-sibling::td`. - **Items that contain something.** A predicate with `.//` looks inside each item: `//article[.//span[contains(@class, "in-stock")]]` keeps only the products in stock. - **The next page.** `//a[@rel="next"]/@href` or `a[rel="next"]::attr(href)`. When there's no `rel`, match the text: `//a[contains(., "Next")]/@href`. - **Structured data.** Many pages carry their product data as JSON-LD. `//script[@type="application/ld+json"]/text()` returns it, and `json.loads()` turns it into a dict, which beats parsing prices out of the layout. - **Meta tags.** `//meta[@property="og:title"]/@content` gets the title the site gives to social networks, which is often cleaner than the H1. - **Sitemaps.** Sitemaps have a default namespace, so `//loc` finds nothing. Use `//ns:url/ns:loc/text()` here, and register the namespace in your code (the snippet does it for you). ## Why does a selector work in DevTools but fail in your scraper? **Because DevTools shows the page after the browser has run its JavaScript and fixed its HTML, and your scraper gets the raw response. A selector copied from the Elements panel can point at content that was never in the HTML, or at a `` the browser added on its own.** Check these in order: - **The content is built by JavaScript.** Search the raw HTML for a word you expect. If it isn't there, fetch the page with a [headless browser](/features/headless-browser/) (`render=true` in Scrape.do) or find the API the page calls. - **`` was added by the browser.** Browsers wrap table rows in ``, lxml doesn't. The tester flags a `tbody` in your path when the pasted HTML has none. `//table//tr` works in both. - **The site served something else.** A bot check, a consent page or a login wall has different HTML. Print `response.status_code` and the start of `response.text` before you blame the selector. - **XPath 2.0.** `ends-with()`, `matches()` and `lower-case()` don't exist in XPath 1.0. The tester names them and shows the 1.0 version. ## Run the selector on a live page **Fetch the page through Scrape.do, then apply the selector with parsel or lxml. Scrape.do handles the proxies, headers and anti-bot checks, and returns the page's HTML, so your selector runs on the same markup a browser would get.** The snippet under the tool builds this for you, with your selector and URL filled in: ```python import requests from parsel import Selector params = { "token": "YOUR_TOKEN", "url": "https://example.com/keyboards/", } response = requests.get("https://api.scrape.do/", params=params) response.raise_for_status() selector = Selector(text=response.text) for value in selector.xpath('//article//h2/a/@href').getall(): print(value) ``` A normal request costs 1 credit, and `render=true` (for pages built by JavaScript) costs 5. The free plan has 1,000 credits a month. ## Related reading - [Web Scraping With Python](/blog/python-web-scraping/), from requests and parsing to the hard parts - [Python Libraries for Web Scraping](/blog/python-web-scraping-library/), including lxml, parsel and Scrapy - [Web Scraping With Scrapy](/blog/advanced-web-scraping-with-scrapy-a-python-tutorial-for-efficient-data-extraction/), where `::text` and `::attr()` come from - [HTML to Markdown Converter](/tools/html-to-markdown/), when you want the whole page as text instead of one field - [HAR File Analyzer](/tools/har-analyzer/), to find the API behind a page instead of parsing its HTML ## FAQ ### Is this XPath and CSS selector tester free? Yes. There is no signup and no limit. The match list shows the first 500 matches (the count covers all of them). The only cap is 5 MB per file, because the parsing runs in your browser tab. To run the same selector on thousands of live pages, use the Python snippet under the tool with the [Scrape.do API](/products/web-scraping-api/). ### Is the HTML I paste uploaded anywhere? No. The page parses your HTML with the browser's own DOMParser and runs the selector with `document.evaluate` (XPath) or `querySelectorAll` (CSS), all inside the tab. The parsed page never runs its scripts or loads its images, and session recording is masked for the tool area. ### Which XPath version does it support? XPath 1.0, the version every browser, lxml and parsel run. XPath 2.0 functions like `ends-with()`, `matches()` and `lower-case()` fail in all of them, so the tester names the function and shows the 1.0 way to write it. ### Does it support Scrapy's ::text and ::attr()? Yes. Browsers don't know these pseudo-elements, so the tester handles them itself: `h1::text` returns the text nodes of each `h1`, and `a::attr(href)` returns each link's `href`, exactly as parsel and Scrapy do. ### Why does my XPath work here but not in my scraper? Usually because your scraper sees different HTML. Content built by JavaScript isn't in the raw HTML (fetch with `render=true`), browsers add `` to tables while lxml doesn't, and some sites serve a block page to scripts. Test against the HTML your scraper actually downloads: print `response.text` and paste it here. ### How do I test an XPath on an XML file with namespaces? Paste the XML or open the file. The tester reads every `xmlns` declaration, keeps each prefix as written, and names the default namespace `ns`, so a sitemap's URLs are `//ns:url/ns:loc`. The prefixes are listed under the tool, and the Python snippet registers the same ones. ### Can I test a selector on a live URL? Not in the browser, because a web page can't fetch another site's HTML for you. Copy the Python snippet instead: it fetches the URL through Scrape.do, which handles proxies and anti-bot checks, and applies your selector with parsel or lxml.