如何使用selenium获取链接href?

时间:2015-10-07 08:43:26

标签: javascript selenium selenium-webdriver

这是我自动化页面上的链接:

enter image description here

这是我检查它的元素:

enter image description here

我想只获得

  

/控制台/ agent_invoices / 2297294

这是href ...

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用getAttribute方法:

String hrefText = yourLinkElement.getAttribute("href");

如果您还需要先找到linkElement,则需要提供更多代码。

鉴于您只有一个链接元素:

WebElement yourLinkElement = driver.findElement(By.tagName("a));

或者例如,如果您已经确定了div-Element,我们在您的问题中看到了结束标记,您可以这样做:

WebElement yourLinkElement = divElement.findElement(By.tagName("a));

答案 1 :(得分:0)

首先找到元素并在变量中获取其属性值,如下所述。

String linkText = webDriver.findElement(By.tagName("a")).getAttribute("href");
相关问题