Skip to content

Add doc about how to get a link by its id or class attribute in DOM crawler page #12141

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
Sep 18, 2019
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: 19 additions & 7 deletions components/dom_crawler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,28 @@ This behavior is best illustrated with examples::
Links
~~~~~

To find a link by name (or a clickable image by its ``alt`` attribute), use
the ``selectLink()`` method on an existing crawler. This returns a ``Crawler``
instance with just the selected link(s). Calling ``link()`` gives you a special
:class:`Symfony\\Component\\DomCrawler\\Link` object::
To find a link by its ``id`` or its ``class`` attribute(s), use the ``filter()``
method on an existing crawler. To find a link by name (or a clickable image by
its ``alt`` attribute), use the ``selectLink()`` method on an existing crawler.
This returns a ``Crawler`` instance with just the selected link(s). Calling
``link()`` gives you a special :class:`Symfony\\Component\\DomCrawler\\Link` object::

$linksCrawler = $crawler->selectLink('Go elsewhere...');
$link = $linksCrawler->link();
// select the link by its id
$linksByIdCrawler = $crawler->filter('#your-link');
$link = $linksByIdCrawler->link();

// select the link by its class
$linksByClassCrawler = $crawler->filter('.link');
$link = $linksByClassCrawler->link();

// select the link by its name
$linksByNameCrawler = $crawler->selectLink('Go elsewhere...');
$linkByName = $linksCrawler->link();

// or do this all at once
$link = $crawler->selectLink('Go elsewhere...')->link();
$linkById = $crawler->filter('#your-link')->link();
$linkByClass = $crawler->filter('.link')->link();
$linkByName = $crawler->selectLink('Go elsewhere...')->link();

The :class:`Symfony\\Component\\DomCrawler\\Link` object has several useful
methods to get more information about the selected link itself::
Expand Down