Skip to content

Can I use XPath selectors in DOM Crawler?

Symfony's DOM Crawler accepts XPath expressions through its filterXPath() method.

OUTPUT FORMATSHTMLJSONMarkdown

You can feed the crawler HTML fetched over HTTP with Guzzle, or pass an HTML string directly, then select nodes with an XPath expression and read their text content.

Short answer

Yes. DOM Crawler exposes XPath selection through filterXPath(). The method returns a node list you can iterate or, for a single match, call text() on to retrieve the element's text content. The same method works whether the crawler was built from a fetched response body or from a literal HTML string.

Loading HTML from an HTTP response

The pipeline has four stages: an HTTP client returns a response, its body is cast to a string, that string becomes the Crawler's input, and filterXPath() runs the expression against the parsed document. In the example below the expression //h1[1] targets the first h1, and text() returns that node's string content. The site base URL is supplied through the SITE_BASE_URL configuration variable rather than hard-coded.

Loading HTML from a string

When the markup is already in hand, the HTTP client drops out of the picture: the string itself is the Crawler's input. Everything downstream — the XPath expression, the returned node list, and the text() call — behaves exactly as it does for a fetched response.

Choosing between XPath and CSS selectors

Use filterXPath() when your selection depends on document position, sibling relationships, or predicates that are awkward in CSS. Use filter() for straightforward class, id, or attribute matches. Both return node lists, so the extraction step with text() is the same.

Failure distinction

An empty result from filterXPath() means the expression matched no nodes in the loaded document. A non-empty result whose text() is empty means the node exists but carries no text content. Check the node count before calling text() on a single expected match to avoid confusing the two cases.

Related DOM Crawler tasks

Other common DOM Crawler operations include finding all links, selecting elements by attribute, class, or multiple tags, working with sibling nodes, scraping tables, and selecting values between two nodes. Each has its own guide in the DOM Crawler question set.

Frequently asked questions

Does DOM Crawler support XPath expressions?

Yes. filterXPath() runs an XPath expression against the loaded document and returns the matching nodes.

Can I use DOM Crawler without Guzzle?

Yes. Pass an HTML string directly to the Crawler constructor and call filterXPath() on it.

How do I get the text of a selected node?

Call text() on the node returned by filterXPath(). For multiple matches, iterate the node list and call text() on each node.