如何从<a> in java using selenium?

时间:2017-05-15 12:16:43

标签: java selenium

I have

<a>
some
<br/>
text
</a>

I need xpath to get "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". How can I get that?

2 个答案:

答案 0 :(得分:0)

我会使用//a[contains(text(),'text')],因为在<a>标记中您有一些空格。

答案 1 :(得分:0)

以下是您完成任务的方法:

//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];\

在您的具体情况下,您也可以使用一个线性:

driver.findElement(By.tagName("a")).getText().split("\\s+")[1]