gartenkralle gartenkralle - 1 month ago 13
Java Question

Receive data from html tag with Java in Selenium

I have the following html tag and I want to receive

"name":"test_1476979972086"
from my
Java Selenium
code.

How can I achive this?

I already tried
getText
and
getAttribute
function but without any success.

<a data-ng-href="#/devices"
target="_blank"
class="ng-binding"
href="#/devices">
{"name":"test_1476979972086"}
</a>


getText()
is always emtpy. The xpath is unique.
newDevice.created
is unique on page.

final By successMessageBy = By.xpath("//p[@data-ng-show='newDevice.created']/a");

final WebElement successMessage = wait.until(ExpectedConditions.presenceOfElementLocated(successMessageBy));

final String msg = successMessage.getText();

Answer

Actually WebElement#getText() returns only visible text. It could be possible element is present there but text would be visible later.

So if WebElement#getText() doesn't work as expected, you should try using getAttribute("textContent") as below:-

successMessage.getAttribute("textContent");
Comments