Skip to content

Commit 48f8437

Browse files
authored
Add doc about how to get a link by its id or class attribute in DOM crawler page
1 parent 13a5281 commit 48f8437

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

components/dom_crawler.rst

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,28 @@ This behavior is best illustrated with examples::
370370
Links
371371
~~~~~
372372

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

378-
$linksCrawler = $crawler->selectLink('Go elsewhere...');
379-
$link = $linksCrawler->link();
379+
// select the link by its id
380+
$linksByIdCrawler = $crawler->filter('#your-link');
381+
$link = $linksByIdCrawler->link();
382+
383+
// select the link by its class
384+
$linksByClassCrawler = $crawler->filter('.link');
385+
$link = $linksByClassCrawler->link();
386+
387+
// select the link by its name
388+
$linksByNameCrawler = $crawler->selectLink('Go elsewhere...');
389+
$linkByName = $linksCrawler->link();
380390

381391
// or do this all at once
382-
$link = $crawler->selectLink('Go elsewhere...')->link();
392+
$linkById = $crawler->filter('#your-link')->link();
393+
$linkByClass = $crawler->filter('.link')->link();
394+
$linkByName = $crawler->selectLink('Go elsewhere...')->link();
383395

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

0 commit comments

Comments
 (0)