我正在使用selenium RC并希望获得所有属性。类似的东西:
link = sel.get_full_link('//a[@id="specific-link"]')
结果将是:
print link
将是:
<a id="specific-link" name="links-name" href="url"> text </a>
这可能吗?
感谢
答案 0 :(得分:4)
这是一个更好的解决方案:
sel.get_eval("window.document.getElementByID('ID').innerHTML")
(不要在javascript上挑剔我。)
答案 1 :(得分:3)
我认为最好的方法是使用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());
}
答案 2 :(得分:1)
String href = selenium.getAttribute(“xpath = // a [@ id =”specific-link“] / @ href”)
答案 3 :(得分:0)
我一直在努力做到这一点,并提出以下建议: -
var selenium = Selenium;
string linkText = selenium.GetText(“// a [@href ='/ admin / design-management']”);
Assert.AreEqual(“Design Management”,linkText);
答案 4 :(得分:0)
使用以下代码获取页面上的所有链接:
$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++;
}
}
答案 5 :(得分:-1)
如果链接不是动态的,那么试试这个相当俗气,讨厌的解决方案(这是在Python中):
selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()
俗气,但它确实有效。
注意:如果链接重定向,则无效。