当我尝试从页面中获取一些不存在的内容时,会捕获此错误:
The current node list is empty. 500 Internal Server Error - InvalidArgumentException
如何安全检查是否存在此内容?这里有一些不起作用的例子:
if($crawler->filter('.PropertyBody')->eq(2)->text()){ // bla bla } if(!empty($crawler->filter('.PropertyBody')->eq(2)->text())){ // bla bla } if(($crawler->filter('.PropertyBody')->eq(2)->text()) != null){ // bla bla }
谢谢,我帮助了自己:
$count = $crawler->filter('.PropertyBody')->count(); if($count > 2){ $marks = $crawler->filter('.PropertyBody')->eq(2)->text(); }
解决方法
你有没有尝试这样的东西?
$text = null; if (!empty($body = $crawler->filter('.PropertyBody'))) { if (!empty($node = $body->eq(2))) { $text = $node->text(); } } $this->assertContains('yourText',$text);