2012-08-07 7 views
19

Quando provo a prendere alcuni contenuti inesistente dalla pagina prendo questo errore:Come posso controllare in sicurezza il nodo vuoto o no? (Symfony 2 Crawler)

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

Come posso controllare, senza rischi esiste questo contenuto o no? Ecco alcuni esempi che non funziona:

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 
} 

GRAZIE, mi sono aiutato con:

$count = $crawler->filter('.PropertyBody')->count(); 
if($count > 2){ 
    $marks = $crawler->filter('.PropertyBody')->eq(2)->text(); 
} 
+1

Grazie uomo. La tua soluzione mi ha salvato molto! –

+0

Il conteggio dei controlli ha aiutato! –

risposta

5

Hai provato qualcosa di simile?

$text = null; 
if (!empty($body = $crawler->filter('.PropertyBody'))) { 
    if (!empty($node = $body->eq(2))) { 
     $text = $node->text(); 
    } 
} 

$this->assertContains('yourText', $text); 
+2

la funzione vuota era ancora esclusa. –

+1

non ha funzionato neanche per me. Ma usando -> count() ha fatto il trucco – SuN

5
$marks = ($crawler->filter('.PropertyBody')->count()) ? $crawler->filter('.PropertyBody')->eq(2)->text() : ''; 
0
try { 
    $text = $crawler->filter('.PropertyBody')->eq(2)->text(); 
} catch (\InvalidArgumentException $e) { 
    // Handle the current node list is empty.. 
} 
Problemi correlati