How can I safely check is node empty or not? (Symfony 2 Crawler)

前端 未结 3 710
-上瘾入骨i
-上瘾入骨i 2021-02-05 03:53

When I try to take some nonexistent content from page I catch this error:

The current node list is empty.
500 Internal Server Error - InvalidArgumentException 
<         


        
相关标签:
3条回答
  • 2021-02-05 04:36
    $marks = $crawler->filter('.PropertyBody')->count()
        ? $crawler->filter('.PropertyBody')->eq(2)->text() 
        : '';
    
    0 讨论(0)
  • 2021-02-05 04:57
    try {
        $text = $crawler->filter('.PropertyBody')->eq(2)->text();
    } catch (\InvalidArgumentException $e) {
        // Handle the current node list is empty..
    }
    
    0 讨论(0)
  • 2021-02-05 04:58

    Have you tried something like this?

    $text = null;
    if (!empty($body = $crawler->filter('.PropertyBody'))) {
        if (!empty($node = $body->eq(2))) {
            $text = $node->text();
        }
    }
    
    $this->assertContains('yourText', $text);
    
    0 讨论(0)
提交回复
热议问题