3

This is the HTML of the web page:

<td class=" gridCell wrapText " style="height:27px;" headers="a15" data-post="" title="">
<div class="oflowDivM ">
<span>
<script src="webwb/pzpega_control_text_12877380907.js!!.js" type="text/javascript"/>
<span class="leftJustifyStyle" data-ctl="Text">30,980,030.0000</span>
</span>
</div>

My intention is to extract the value 30,980,030.0000 I am using the following code:

driver.findElement(By.xpath("//td/div/span/span")).getText();

However, the getText() returns an empty string. The xpath is correct as I do not get NoSuchElementException. Could you please help in identifying my mistake here?

10
  • why not try by class name only...driver.findElement(By.classname("leftJustifyStyle")).getText(); Commented Nov 3, 2016 at 12:21
  • I tried that as well. But it results in an empty string. The xpath works fine but getText() returns an empty string Commented Nov 3, 2016 at 12:25
  • can you share the link to html page Commented Nov 3, 2016 at 12:28
  • Sorry, the website opens only in my client network Commented Nov 3, 2016 at 12:31
  • 1
    Try System.out.println(driver.findElement(By.xpath("//td/div/span/span")).getAttribute("outerHTML"));. Is it printing the element you are expecting? If it does, put a breakpoint on that line, let the script run until it breaks, and then run the line... does it work now? If so, you probably need a wait in there. Commented Nov 3, 2016 at 13:14

5 Answers 5

6

Can you please try below and see if value is printed or not:

driver.findElement(By.xpath("//td/div/span/span")).getAttribute("innerText");

If it still happens, can you try to have few seconds of wait and then try yo find element and get the text.

Sign up to request clarification or add additional context in comments.

3 Comments

driver.findElement(By.xpath("//td/div/span/span")).getAttribute("innerText"); returns a NullPointerException. Adding wait also results in an empty string being returned.
can you try to store this element into a variable of WebElement type and then check if it is printing any of its defined attribute or not.
Also, check if element is visible or not using element.isDisplayed(); print this value and see what value it prints
5

I probably think your element is hidden so getText() returns blank. The comment from @JeffC should fix your problem. Otherwise, try code below:

Thread.sleep(5000);
string text = driver.findElement(By.cssSelector("td > div span.leftJustifyStyle")).getAttribute("innerText");

or find only span which contains string that is longer than 2 characters

Thread.sleep(5000);
string text = driver.findElement(By.xpath("//td/div/span/span[string-length(normalize-space(text())) > 2]")).getAttribute("innerText");

Comments

0

Try this:

String details = "";

Thread.sleep(3000);

details = driver.findElement(By.xpath("//td/div//span")).getAttribute("value");

6 Comments

This results in NullPointerException as there is no attribute "value" as seen from the html
"30,980,030.0000" is this the text you actually needed
can you please get me the page,please attach link
try this: driver.findElement(By.xpath(.//*div[@class='oflowDivM']//span//span)).getText(); or driver.findElement(By.xpath(.//*div[@class='oflowDivM']//span//span)).getAttribute("value");
unfortunately the website opens only in client network. The xpath did not work. I suspect that there is something that is stopping getText() from reading the value as the element is getting identified by all the xpaths tried, however the getText() returns empty string
|
0
String amount = driver.findElement(By.xpath(".//*td[@class='gridCell wrapText']//div[@class='oflowDivM']//span//span[@class='leftJustifyStyle']")).getText();

This should work - just make sure class names are correct as in HTML...

2 Comments

Empty string here as well :(
edited my answer - try again...also please cross check the class names used here with actual class names in HTML
0

From what I found out getText() sometimes returns empty string (if the element is hidden for example. I'm not sure if is or not the only case and neither about the cause).

Anyway what worked for me was getAttribute("textContent").

So how about something like :

driver.findElement(By.xpath("//td/div/span/span")).getAttribute("textContent");

?

Comments

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.