2012-03-26 95 views
0

i've got a question, regarding selenium + php (using PHPUnit_Extensions_SeleniumTestCase):php + selenium,如何获取全部<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 ?

回答

1

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

返回页面上所有链接的ID。如果给定的链接没有ID, 它将在此数组中显示为“”。



取而代之的是你可以使用JavaScript的所有链接(看getElementsByTagName() - example)。

编辑
OK,我已经做了你(我在做类似的东西);)

$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);