[英]How can I get the text from <a> in java using selenium?
I have 我有
<a>
some
<br/>
text
</a>
I need xpath to get "text"
. 我需要xpath来获取"text"
。 I have used 我用过
String getLinkText="//a[1]";
browser.getCurrentWebDriver().findElement(By.xpath(getLinkText)).getText();
It is giving "some"
as a output but I need "text"
. 它给"some"
作为输出,但我需要"text"
。 How can I get that? 我该怎么办?
我会使用//a[contains(text(),'text')]
只是因为<a>
标记中有一些空格。
Here's how you could do your task: 这是您可以完成任务的方式:
//First, get the full link text (your locator may vary)
String linkText = driver.findElement(By.tagName("a")).getText();
//Split the text by spaces
String [] splittedLink = linkText.split("\\s+");
//Get the last part of your text
String lastPartOfLink = splittedLink[splittedLink.length - 1];\
In your specific case you could use the one linear too: 在您的特定情况下,您也可以使用一个线性:
driver.findElement(By.tagName("a")).getText().split("\\s+")[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.