siblings() error: InvalidArgumentException: The current node list is empty

siblings() error: InvalidArgumentException: The current node list is empty

我在 symfony2 单元测试中遇到错误,symfony : 2.7.1

when I use siblings() I have a the error:

InvalidArgumentException: 当前节点列表为空。 树枝文件:

<h1>ddd</h1>

<p>ahmedghgh</p>

<ul>
    <li>dddd</li>
    <li>eeee</li>
    <li>ffff</li>    
</ul>

</p>bye</p>

<form action ="" method="GET" name ="nameForm">
    <input type="text" value ="name" name="name">
     <input type="submit" value ="send" name="send">

</form>

BasicControllerTest.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace TestingSymfony\BasicBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class BasicControllerTest extends WebTestCase {

    public function testHelloContent() {
        $client = static::createClient();
        $crawler = $client->request('GET', '/helloworld');
        $h1 = $crawler->filter('h1')->eq(0);
        $p1 = $crawler->filter('p')->first();


        $ul = $p1->siblings()->eq(0);        
        $l1 = $ul->children()->first();
        $l2 = $ul->children()->eq(1);
        $l3 = $ul->children()->last();

        $p2 = $crawler->filterXPath("//p")->last();
    }
}

删除兄弟姐妹后,一切正常,没有出现错误

你的twig文件有错别字:检查P标签是否正确打开和关闭,如下:

<p>bye</p>

而不是

</p>bye</p>

希望对您有所帮助

您的 filter 没有 return 任何结果,因为您有错字。这就是它崩溃的原因。但是,对于没有任何错字的其他人,这就是我通过添加 try catch 来解决这个问题的方法。

try {
   $p1 = $crawler->filter('p')->first();
} catch (\InvalidArgumentException $e) {
    // Handle the current node list is empty..
}