Skip to content

Commit c742ce9

Browse files
committed
Merge branch '4.4'
* 4.4: Add doc about how to get a link by its id or class attribute in DOM crawler page
2 parents 3d3a194 + fddff98 commit c742ce9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

components/dom_crawler.rst

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,26 @@ This behavior is best illustrated with examples::
396396
Links
397397
~~~~~
398398

399-
To find a link by name (or a clickable image by its ``alt`` attribute), use
400-
the ``selectLink()`` method on an existing crawler. This returns a ``Crawler``
401-
instance with just the selected link(s). Calling ``link()`` gives you a special
402-
:class:`Symfony\\Component\\DomCrawler\\Link` object::
403-
404-
$linksCrawler = $crawler->selectLink('Go elsewhere...');
405-
$link = $linksCrawler->link();
406-
407-
// or do this all at once
408-
$link = $crawler->selectLink('Go elsewhere...')->link();
399+
Use the ``filter()`` method to find links by their ``id`` or ``class``
400+
attributes and use the ``selectLink()`` method to find links by their content
401+
(it also finds clickable images with that content in its ``alt`` attribute).
402+
403+
Both methods return a ``Crawler`` instance with just the selected link. Use the
404+
``link()`` method to get the :class:`Symfony\\Component\\DomCrawler\\Link` object
405+
that represents the link::
406+
407+
// first, select the link by id, class or content...
408+
$linkCrawler = $crawler->filter('#sign-up');
409+
$linkCrawler = $crawler->filter('.user-profile');
410+
$linkCrawler = $crawler->selectLink('Log in');
411+
412+
// ...then, get the Link object:
413+
$link = $linkCrawler->link();
414+
415+
// or do all this at once:
416+
$link = $crawler->filter('#sign-up')->link();
417+
$link = $crawler->filter('.user-profile')->link();
418+
$link = $crawler->selectLink('Log in')->link();
409419

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

0 commit comments

Comments
 (0)