How to find elements without specific attributes in DOM Crawler?

You have two options to find elements without specific attributes in DOM Crawler. The first option uses the filterXPath method with an XPath selector that includes a negative predicate. And the second option uses the filter method with the :not CSS pseudo-class and the attribute selector.

Here is some sample code that showcases the filterXPath options and finds all img tags that do not have an alt attribute:

use Symfony\Component\DomCrawler\Crawler;

$html = <<<EOD
<!DOCTYPE html>
<html>
<head>
	<title>Example Page</title>
</head>
<body>
	<h1>Hello, world!</h1>
	<p>This is an example page.</p>
	<img src="logo.png" />
  <img src="header.png" alt="header"/>
  <img src="yasoob.png" alt="profile picture"/>
</body>
</html>
EOD;

// Load the HTML document
$crawler = new Crawler($html);

// Find all img elements without an alt attribute
$imagesWithoutAlt = $crawler->filterXPath('//img[not(@alt)]');

// Loop over the images and print their src attributes
foreach ($imagesWithoutAlt as $image) {
    echo $image->getAttribute('src') . PHP_EOL;
}

// Output:
// logo.png

Here is some sample code that uses the filter method with :not CSS pseudo-class instead:

use Symfony\Component\DomCrawler\Crawler;

$html = <<<EOD
<!DOCTYPE html>
<html>
<head>
	<title>Example Page</title>
</head>
<body>
	<h1>Hello, world!</h1>
	<p>This is an example page.</p>
	<img src="logo.png" />
  <img src="header.png" alt="header"/>
  <img src="yasoob.png" alt="profile picture"/>
</body>
</html>
EOD;

// Load the HTML document
$crawler = new Crawler($html);

// Find all img elements without an alt attribute
$imagesWithoutAlt = $crawler->filter('img:not([alt])');

// Loop over the images and print their src attributes
foreach ($imagesWithoutAlt as $image) {
    echo $image->getAttribute('src') . PHP_EOL;
}

// Output:
// logo.png

Related DOM Crawler web scraping questions: