简体   繁体   English

PHP + Selenium,如何<a href…>从一页</a>获取所有<a href…>标签</a>

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

i've got a question, regarding selenium + php (using PHPUnit_Extensions_SeleniumTestCase): 我有一个关于硒+ php的问题(使用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 而且并没有找到所有元素:(如果我尝试通过$ this-> getText()获取内容,则将填充很少的内容,有些则为空,并且标记的总量比我在页面上实际的少

anyone got an idea what i might be doing wrong ? 有人知道我可能做错了什么吗?

There is a very useful method in Selenium - getAllLinks() . Selenium中有一个非常有用的方法getAllLinks() Look here . 这里

Returns the IDs of all links on the page. 返回页面上所有链接的ID。 If a given link has no ID, it will appear as "" in this array. 如果给定的链接没有ID,则在此数组中将显示为“”。



Instead of this you can get all links using javascript (look at getElementsByTagName() - example ). 取而代之的是,您可以使用javascript获取所有链接(请getElementsByTagName() - 示例 )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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