2012-03-26 7 views
0

i've got a question, regarding selenium + php (using PHPUnit_Extensions_SeleniumTestCase):php + selen, wie man alle bekommt <a href...> tags from one page

i'm going through a loop, trying to get all elements from a webpage, by doing something like:

$i = 1; 
while ($this->isElementPresent("//a[" . $i . "]")) { 
     $tagContents = $this->getText("//a[" . $i . "]"); 
     print $tagContents . "\n"; 
     $i++; 
} 

and it's not finding all elements :( if i try to get the contents via $this->getText() a very few are filled, some are empty, and the overall amount of tags is way less than i really have on my page

anyone got an idea what i might be doing wrong ?

Antwort

1

There is a very useful method in Selenium - getAllLinks(). Look here.

Gibt die IDs aller Links auf der Seite zurück. Wenn ein bestimmter Link keine ID hat, wird in diesem Array als "" angezeigt.



dieser Stattdessen können Sie alle Verbindungen erhalten mit Hilfe von Javascript (siehe getElementsByTagName()-example).

EDIT
OK, ich habe es getan für Sie (Ich arbeitete an etwas ähnliches);)

$js = "function getAllLinks() { 
      var links = window.document.getElementsByTagName('a'); 
      var contents = []; 
      for (i = 0; i < links.length; i++) { 
       var link = links[i]; 
       var text = link.textContent; 
       contents.push(text); 
      } 
      return contents; 
     } 
     getAllLinks();"; 
$links = $this->getEval($js); 
Verwandte Themen