我正在使用selenium RC并希望获得所有属性。类似的东西:

link = sel.get_full_link('//a[@id="specific-link"]')

结果将是:

print link

将是:

<a id="specific-link" name="links-name" href="url"> text </a>

这可能吗?

感谢

有帮助吗?

解决方案

这是一个更好的解决方案:

sel.get_eval("window.document.getElementByID('ID').innerHTML")

(不要在javascript上挑剔我。)

其他提示

我认为最好的方法是使用getHtmlSource命令获取整个HTML源代码,然后使用正则表达式或HTML解析器来提取感兴趣的元素。

以下Java示例将输出到System.out的所有链接:

selenium.open("http://www.example.com/");
String htmlSource = selenium.getHtmlSource();
Pattern linkElementPattern = Pattern.compile("<a\\b[^>]*href=\"[^>]*>(.*?)</a>");
Matcher linkElementMatcher = linkElementPattern.matcher(htmlSource);
while (linkElementMatcher.find()) {
    System.out.println(linkElementMatcher.group());
}

getAttribute

String href = selenium.getAttribute(&quot; xpath = // a [@ id =&quot; specific-link&quot;] / @ href&quot;)

我一直在努力做到这一点,并提出以下建议: -

var selenium = Selenium;

string linkText = selenium.GetText(&quot; // a [@href ='/ admin / design-management']&quot;);

Assert.AreEqual(&quot; Design Management&quot;,linkText);

使用以下代码获取页面上的所有链接:

$str3= "window.document.getElementsByTagName('a')";
$k = $this->selenium->getEval($str3);
$url = explode(",",$k);
$array_size = count($url);
$name=array();
$l=0;
for($i=0;$i<$array_size;$i++)
{
    if(!strstr($url[$i], 'javascript'))
    {
        $name[$l]=$url[$i];

        echo "\n".$name[$l];
        $l++;
    }
}

如果链接不是动态的,那么试试这个相当俗气,讨厌的解决方案(这是在Python中):

selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()

俗气,但它确实有效。

注意:如果链接重定向,则无效。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top