Skip to content

[DomCrawler] Add more details about some methods #21034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions testing/dom_crawler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@ selects the last one on the page, and then selects its immediate ancestor elemen
Many other methods are also available:

``filter('h1.title')``
Nodes that match the CSS selector.
Finds nodes that match the given CSS selector (which must be supported by
Symfony's :doc:`CSS Selector component </components/css_selector>`).
``filterXpath('h1')``
Nodes that match the XPath expression.
Finds nodes matching the given `XPath expression`_.
``eq(1)``
Node for the specified index.
Returns the node at the given index (``0`` is the first node).
``first()``
First node.
Returns the first node (equivalent to ``eq(0)``).
``last()``
Last node.
Returns the last node.
``siblings()``
Siblings.
Returns all sibling nodes (nodes with the same parent, excluding the current node).
``nextAll()``
All following siblings.
Returns all following siblings (same parent, after the current node).
``previousAll()``
All preceding siblings.
Returns all preceding siblings (same parent, before the current node).
``ancestors()``
Returns the ancestor nodes.
Returns all ancestor nodes (parents, grandparents, etc., up to the ``<html>``
element).
``children()``
Returns children nodes.
Returns all direct child nodes of the current node.
``reduce($lambda)``
Nodes for which the callable does not return false.
Filters the nodes using a callback; keeps only those for which it returns ``true``.

Since each of these methods returns a new ``Crawler`` instance, you can
narrow down your node selection by chaining the method calls::
Expand Down Expand Up @@ -91,3 +93,5 @@ The Crawler can extract information from the nodes::
$data = $crawler->each(function ($node, int $i): string {
return $node->attr('href');
});

.. _`XPath expression`: https://developer.mozilla.org/en-US/docs/Web/XML/XPath