Java 获取 href 值(WebDriver)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20579007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get href value (WebDriver)
提问by user3062055
How do I get a value from href?
如何从 href 获取值?
like this eg:
像这样例如:
<div id="cont"><div class="bclass1" id="idOne">Test</div>
<div id="testId"><a href="**NEED THIS VALUE AS STRING**">
<img src="img1.png" class="clasOne" />
</a>
</div>
</div>
</div>
I need that value as string.
我需要该值作为字符串。
I've tried with this:
我试过这个:
String e = driverCE.findElement(By.xpath("//div[@id='testId']")).getAttribute("href");
JOptionPane.showMessageDialog(null, e);
But just returns NULL value...
但只是返回 NULL 值...
回答by pavanraju
You have pointed your element to 'div' instead of 'a'
您已将元素指向 'div' 而不是 'a'
Try the below code
试试下面的代码
driverCE.findElement(By.xpath("//div[@id='testId']/a")).getAttribute("href");
回答by Nishant
If you got more than one anchor tag, the following code snippet will help to find all the links pointed by href
如果你有多个锚标签,下面的代码片段将有助于找到所有指向的链接 href
//find all anchor tags in the page
List<WebElement> refList = driver.findElements(By.tagName("a"));
//iterate over web elements and use the getAttribute method to
//find the hypertext reference's value.
for(WebElement we : refList) {
System.out.println(we.getAttribute("href"));
}