简体   繁体   中英

php + selenium, how to get all <a href…> tags from one page

i've got a question, regarding selenium + php (using PHPUnit_Extensions_SeleniumTestCase):

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 ?

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

Returns the IDs of all links on the page. If a given link has no ID, it will appear as "" in this array.



Instead of this you can get all links using javascript (look at getElementsByTagName() - example ).

EDIT
OK, I have done it for you (I was working on something similar) ;)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM