Webdriver: getText() returning empty string

5,524 views
Skip to first unread message

Smita Sinha

unread,
Jul 24, 2012, 4:43:12 AM7/24/12
to seleniu...@googlegroups.com
Hi All,

My HTML code is:
<span id="A0948:iceform:j_idt247" class="iceOutTxt  alert">Error text</span>

I want to get the text "Error text"

My code:
WebElement webEl = driver.findElement(By.xpath("//span[@id = 'A0948:iceform:j_idt247']"));
System.out.println("Get Text:"+webEl.getText());

The output is null
Get Text:

Someone please help me in resolving this.

Thanks,
Smita

Peter Gale

unread,
Jul 24, 2012, 4:59:38 AM7/24/12
to Selenium Users
What happens if you try to get the class attribute of this element?


Date: Tue, 24 Jul 2012 14:13:12 +0530
Subject: [selenium-users] Webdriver: getText() returning empty string
From: sayhi....@gmail.com
To: seleniu...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Smita Sinha

unread,
Jul 24, 2012, 5:11:59 AM7/24/12
to seleniu...@googlegroups.com
Hi Peter,
I am getting the class attribute.

O/P
Class: iceOutTxt  alert

Code:
WebElement webEl = driver.findElement(By.xpath("//span[@id = 'A0948:iceform:j_idt247']"));
System.out.println("Class:"+ webEl.getAttribute("class") );

Thanks,
Smita

Smita Sinha

unread,
Jul 24, 2012, 5:13:31 AM7/24/12
to seleniu...@googlegroups.com
I even tried this.
 text = (String) ((JavascriptExecutor)driver).executeScript("document.getElementById(\"A0948:iceform:j_idt247\").innerText");
 System.out.println("Text Inside:"+text);

O/P
Text Inside:null

Don`t know whats wrong.

Thanks,
Smita

Mark Collin

unread,
Jul 24, 2012, 5:21:52 AM7/24/12
to seleniu...@googlegroups.com

Are there two elements on the page with the same ID (A bug if there are)?  If so you could be pulling the text from the wrong one.

Smita Sinha

unread,
Jul 24, 2012, 5:24:51 AM7/24/12
to seleniu...@googlegroups.com
No Mark,
There is only one element on the page with this id (Using firefinder) and 
webEl.isEnabled() returns true.


-Smita

Peter Gale

unread,
Jul 24, 2012, 5:27:19 AM7/24/12
to Selenium Users
Does the same type of code work on other SPAN elements?

I'm not sure to what extent span tags are different to other tags and only affect part of the text in the parent, i.e. they can contain no text themselves.

Try to get the text of the parent object:

WebElement webEl = driver.findElement(By.xpath("//span[@id = 'A0948:iceform:j_idt247']/.."));
System.out.println("Get Text:"+webEl.getText());


Date: Tue, 24 Jul 2012 14:43:31 +0530
Subject: Re: [selenium-users] Webdriver: getText() returning empty string

Smita Sinha

unread,
Jul 24, 2012, 6:02:41 AM7/24/12
to seleniu...@googlegroups.com
Thankyou Mark and Peter,

Clicking on the element before getText() has resolved this issue.

-Smita

madeja madeja

unread,
Oct 1, 2015, 10:20:57 AM10/1/15
to Selenium Users
Hi, this click totaly help me... thanks

Dňa utorok, 24. júla 2012 12:02:41 UTC+2 sayhi_...@yahoo.com napísal(-a):

Ripon Al Wasim

unread,
Oct 2, 2015, 1:47:09 AM10/2/15
to seleniu...@googlegroups.com
Hi,
Can you please provide the HTML code snippet for this?

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages