These methods on the Crawler are intended to initially populate your
Crawler and aren't intended to be used to further manipulate a DOM
(though this is possible). However, since the Crawler is a set of
DOMElement objects, you can use any method or property available
on DOMElement, DOMNode or DOMDocument.
For example, you could get the HTML of a Crawler with something like
this:

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
Link object:

1
2
3
4
5

$linksCrawler=$crawler->selectLink('Go elsewhere...');$link=$linksCrawler->link();// or do this all at once$link=$crawler->selectLink('Go elsewhere...')->link();

The Link object has several useful
methods to get more information about the selected link itself:

1
2

// return the proper URI that can be used to make another request$uri=$link->getUri();

Note

The getUri() is especially useful as it cleans the href value and
transforms it into how it should really be processed. For example, for a
link with href="#foo", this would return the full URI of the current
page suffixed with #foo. The return from getUri() is always a full
URI that you can act on.

Special treatment is also given to forms. A selectButton() method is
available on the Crawler which returns another Crawler that matches a button
(input[type=submit], input[type=image], or a button) with the
given text. This method is especially useful because you can use it to return
a Form object that represents the
form that the button lives in:

1
2
3
4
5
6

$form=$crawler->selectButton('validate')->form();// or "fill" the form fields with data$form=$crawler->selectButton('validate')->form(array('name'=>'Ryan',));

The Form object has lots of very
useful methods for working with forms:

1
2
3

$uri=$form->getUri();$method=$form->getMethod();

The getUri() method does more
than just return the action attribute of the form. If the form method
is GET, then it mimics the browser's behavior and returns the action
attribute followed by a query string of all of the form's values.

You can virtually set and get values on the form:

1
2
3
4
5
6
7
8
9
10
11
12

// set values on the form internally$form->setValues(array('registration[username]'=>'symfonyfan','registration[terms]'=>1,));// get back an array of values - in the "flat" array like above$values=$form->getValues();// returns the values like PHP would see them,// where "registration" is its own array$values=$form->getPhpValues();

// Set a single field$form->setValues(array('multi'=>array('value')));// Set multiple fields at once$form->setValues(array('multi'=>array(1=>'value','dimensional'=>'an other value')));

This is great, but it gets better! The Form object allows you to interact
with your form like a browser, selecting radio values, ticking checkboxes,
and uploading files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

$form['registration[username]']->setValue('symfonyfan');// check or uncheck a checkbox$form['registration[terms]']->tick();$form['registration[terms]']->untick();// select an option$form['registration[birthday][year]']->select(1984);// select many options from a "multiple" select or checkboxes$form['registration[interests]']->select(array('symfony','cookies'));// even fake a file upload$form['registration[photo]']->upload('/path/to/lucas.jpg');

What's the point of doing all of this? If you're testing internally, you
can grab the information off of your form as if it had just been submitted
by using the PHP values:

1
2

$values=$form->getPhpValues();$files=$form->getPhpFiles();

If you're using an external HTTP client, you can use the form to grab all
of the information you need to create a POST request for the form:

1
2
3
4
5
6

$uri=$form->getUri();$method=$form->getMethod();$values=$form->getValues();$files=$form->getFiles();// now use some HTTP client and post using this information

One great example of an integrated system that uses all of this is Goutte.
Goutte understands the Symfony Crawler object and can use it to submit forms
directly:

1
2
3
4
5
6
7
8
9
10
11
12
13

useGoutte\Client;// make a real request to an external site$client=newClient();$crawler=$client->request('GET','https://github.com/login');// select the form and fill in some values$form=$crawler->selectButton('Log in')->form();$form['login']='symfonyfan';$form['password']='anypass';// submit that form$crawler=$client->submit($form);