Manipulating Pages

Once you get a page element, you will want to manipulate it. You can also interact with the page, which is covered in the next chapter.

Getting the tag name

The NodeElement::getTagName method allows you to get the tag name of the element. This tag name is always returned lowercased.

$el = $page->find('css', '.something');

// get tag name:
echo $el->getTagName(); // displays 'a'

Accessing HTML attributes

The NodeElement class gives you access to HTML attributes of the element.

Checks whether the element has a given attribute.
Gets the value of an attribute.
Checks whether the element has the given class (convenience wrapper around getAttribute('class')).
$el = $page->find('css', '.something');

if ($el->hasAttribute('href')) {
    echo $el->getAttribute('href');
} else {
    echo 'This anchor is not a link. It does not have an href.';

Element Content and Text

The Element class provides access to the content of elements.

Gets the inner HTML of the element, i.e. all children of the element.
Gets the outer HTML of the element, i.e. including the element itself.
Gets the text of the element.


getText() will strip tags and unprinted characters out of the response, including newlines. So it’ll basically return the text that the user sees on the page.

Checking Element Visibility

The NodeElement::isVisible method checks whether the element is visible.

Accessing Form State

The NodeElement class allows access to the state of form elements:

Gets the value of the element. See Interacting with Forms.
Checks whether the checkbox or radio button is checked.
Checks whether the <option> element is selected.

Shortcut methods

The TraversableElement class provides a few shortcut methods that allow finding a child element in the page and checking the state of it immediately:

Looks for a checkbox (see findField) and checks whether it is checked.
Looks for a checkbox (see findField) and checks whether it is not checked.