Rqgg Page 1/2

bemutefrogtownΑσφάλεια

18 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

88 εμφανίσεις

Symfony DomCrawler: Find element with specific attribute value
Asked by
Wesley
on 2013-04-30T09:29:05-04:00
I'm using the DomCrawler component:
http://symfony.com/doc/current/components/dom_crawler.html

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
1:


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


$link->filter('img')->attr('src');
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
M8R-1jmw5r
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
two
software libraries
dom-crawler
and
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));
var_dump($link->filter('img')->attr('src'));
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
count()
and the second is the src attribute value.

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

Archives
Technology
Programmers
Apr 30th, 2013
Week 18, 2013
April, 2013
Rqgg
Page 1/2
Symfony DomCrawler: Find element with specific attribute value
Related
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
http://www.rqgg.net/topic/psqrs-symfony-domcrawler-find-element-with-specific-attribute-value.html
Rqgg
Page 2/2