Selenium (Java) Get FULL text from shortened displayed on page

Viewed 121

I want to get the FULL text of link, that is contained in a page element and retrieve is value possibly with .getText() parameter. Problem is, that on the page there is only part of it displayed followed by "...".

.getText returns = "http://www.example.com/es_es/spa..."
what I need to get ="http://www.example.com/es_es/spain/legal-notice.html"

this is HTML

<a href="http://www.example.com/es_es/spain/legal-notice.html" 

target="_blank">http://www.example.com/es_es/spa...</a>

I am able to locate the element, just not to retrieve its full text. I tried .getAttribute("href") but it returns "null".

Thank you!

2 Answers

Try this using xpath :

WebElement hrefText=driver.findElement(By.xpath("//a[@href='http://www.example.com/es_es/spain/legal-notice.html']"));
String text_hrefText=hrefText.getAttribute("href");
System.out.println(text_hrefText);

You wanted to use getText() method, but getText() will be helpfull while we want to retrieve the innerHTML only. But if we use getAttribute() method, we will be able to pull out any of the attributes of the WebElement.

Now, to retrieve the full text of href attribute you can use the following line of code :

System.out.println(driver.findElement(By.xpath("//a[contains(@href,'example.com/es_es/spain/legal-notice')]")).getAttribute("href"));
Related