0

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();
5
  • What do you mean without any success.?? Is there any exception?? And share how did you tried as well?? Commented Oct 20, 2016 at 16:40
  • edited...see above Commented Oct 20, 2016 at 16:50
  • Is this element text visible on the page?? Commented Oct 20, 2016 at 16:53
  • Update the html one level up ie the p tag... Commented Oct 20, 2016 at 16:53
  • And try once as wait.until(ExpectedConditions.visibilityOfElementLocated(successMessageBy)).getText(); and let me know.. Commented Oct 20, 2016 at 16:55

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! Right element is not visible but present! :)
0

upon first glance, the below should work. the fact that what you've tried doesnt work, leads me to believe that you aren't selecting the correct element. since i am ignorant of the rest of your html, this might not be unique. you'll have to play around with it, or share the surrounding html

String json = driver.findElement(By.cssSelector("a[href$='/devices']")).getText()

7 Comments

The selector is unique but getText returns an empty string.
@gartenkralle I copied the HTML and the code from the answer above and got the text....
interesting...I'm not.
then something else is going on. you'll have to show more context for the element. also, is that element you are sharing - is it from inspect element? or view page source? i ask this last question because the source is probably different than the actual because of all of the DHTML
Can you add a screenshot from the inspect element from the surrounding p tag that you are using in the xpath...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.