Symfony DomCrawler: Find element with specific attribute value

前端 未结 1 2083
不思量自难忘°
不思量自难忘° 2020-12-25 13:24

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 specif

相关标签:
1条回答
  • 2020-12-25 13:46

    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 css-selector, the example code you provided works just fine considering the following example HTML:

    <?php
    use Symfony\Component\DomCrawler\Crawler;
    
    // require dependencies here    
    
    $html = <<<'HTML'
    <!DOCTYPE html>
    <html>
        <body>
            <p class="message">Hello World!</p>
            <p>Hello Crawler!</p>
            <div id="product">
                <a data-type="bla">
                    <img src="OK">
                </a>
            </div>
        </body>
    </html>
    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?

    0 讨论(0)
提交回复
热议问题