简体   繁体   中英

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" . 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?

我会使用//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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM