Rqgg Page 1/2


18 nov. 2013 (il y a 5 années et 7 mois)

128 vue(s)

Symfony DomCrawler: Find element with specific attribute value
Asked by
on 2013-04-30T09:29:05-04:00
I'm using the DomCrawler component:

I'd like to, using the CSS like syntax, get an element with a specific attribute value.

Here's the code I'm using:

$link = $crawler->filter('#product a[data-type="bla"]');
This seems to work, as the following returns

echo count($link);
However, I can never not filter further than this. I can not do:

This results in the following error message:

The current node list is empty.
However, I know for certain that it isn't.

I've tried the syntax on other elements and it's always the same. I am doing something wrong or is
this not possible (with css like syntax, not xpath)

Best Answer
Answer by
on 2013-04-30T09:42:02-04:00
I can not follow your problem. Using the current development versions (and also 2.1.0 and 2.2.0
versions) of the
software libraries
https://packagist.org/packages/symfony/css-selector, the example code you provided works just fine
considering the following example HTML:

Hello World!

Hello Crawler!

HTML; $crawler = new Crawler($html); $link =
$crawler->filter('#product a[data-type="bla"]'); echo var_dump(count($link));
As you can see this is exactly your code (only a little
different but essentially not), which gives the following output verbatim:

int(1) string(2) "OK"
The first output line is the
and the second is the src attribute value.

Have you run composer update? Have you double-checked the input?

Apr 30th, 2013
Week 18, 2013
April, 2013
Page 1/2
Symfony DomCrawler: Find element with specific attribute value
Add node to existing XML document using DOm and PHP
PHP Simple HTML Dom Parser find() crashing while traversing past a null element
Change an atomic value in an XML file using the Php XML DOM
Use PHP Simple HTML DOM Parser to find table cell and get contents of next sibling
PHP Simple HTML DOM - Get text inside tag
PHP DOM parser for HTML and CSS
Parse HTML with PHP DOM - Class contains text
PHP Security on Forms and DOM Manipulation
PHP Simple HTML DOM Parser div selector
Using cURL and dom to scrape data with php
View Online
Page 2/2