Skip to content

How to find HTML elements by attribute with DOM Crawler

When the element you need is identified by an attribute rather than a tag or class, DOM Crawler's filterXPath method accepts an XPath expression containing an attribute predicate.

OUTPUT FORMATSHTMLJSONMarkdown

The predicate narrows the node set to elements whose attribute equals a chosen value, and each matched node can then be inspected for its attribute content.

The attribute-predicate approach

DOM Crawler exposes XPath filtering through filterXPath. An expression such as //input[@type="email"] selects every input element whose type attribute is exactly email. The @ symbol introduces the attribute name, and the bracketed condition is the predicate that must hold for a node to be returned.

This approach is useful when the identifying signal lives in an attribute value: a form control's type, a link's rel, or a data-* marker. The predicate can be combined with a tag restriction so the match stays scoped to the element type you expect.

Worked example: reading placeholder text from email inputs

The example below fetches a login page, loads the response body into a Crawler, filters for input elements with type="email", and prints each matched input's placeholder attribute. The HTTP request is made with Guzzle, so that package must be installed for the snippet to run as written.

The exact code is preserved in the accompanying protected module code0001. Read it as a sequence: build the client, request the page, cast the body to a string, construct the Crawler, apply the XPath predicate, iterate the matches, and read the attribute.

Field and control map of the example

Use this map to adapt the snippet to a different attribute or page:

  • HTTP client: Guzzle client instance; performs the GET request.
  • Request target: the login URL supplied in the example; replace with the page you are parsing.
  • Response body: cast to string and passed to the Crawler constructor.
  • Crawler: the DOM Crawler object that holds the parsed document.
  • filterXPath expression: //input[@type="email"]; change the tag, attribute name, or value to retarget the selection.
  • Iteration: foreach over the matched nodes.
  • Attribute read: getAttribute('placeholder'); substitute the attribute you want to extract.

Choosing the right filter

Use filterXPath with an attribute predicate when the selection depends on an attribute value. If the selection depends on a class, a tag, or a structural relationship, other DOM Crawler filters are usually more direct. The related questions below cover those alternatives, including class-based selection, multiple tags, sibling nodes, and selecting values between nodes.

Related DOM Crawler questions

The following related topics extend the attribute-selection technique:

  • Can I use XPath selectors in DOM Crawler?
  • How to find all links using DOM Crawler and PHP?
  • How to find elements without specific attributes in DOM Crawler?
  • How to find HTML elements by class with DOM Crawler?
  • How to find HTML elements by multiple tags with DOM Crawler?
  • How to find sibling HTML nodes using DOM Crawler and PHP?
  • How to scrape tables with DOM Crawler?
  • How to select values between two nodes in DOM Crawler and PHP?

For broader PHP scraping background, see the linked PHP web scraping tutorial.

Frequently asked questions

Does filterXPath require the attribute to exist on every matched node?

The predicate only returns nodes where the attribute condition holds. If you then call getAttribute for a different attribute that a matched node lacks, that read can fail or return nothing depending on the DOM Crawler behavior for missing attributes. Keep the predicate and the extracted attribute aligned, or guard the read.

What is the difference between no matches and a missing attribute?

No matches means the XPath expression returned an empty node set, so the loop body never runs. A missing attribute means a node was matched but the attribute you asked for is absent on that node. Check the node count first, then check the attribute on each node.

Can I match an attribute value other than email?

Yes. Replace the tag, attribute name, and value inside the predicate. For example, //a[@rel="nofollow"] selects anchor elements whose rel attribute equals nofollow. The same filterXPath method applies.

Do I have to use Guzzle to load the HTML?

No. The example uses Guzzle to fetch the page, but DOM Crawler only needs an HTML string. You can supply the markup from any HTTP client or from a local file.