Selenium getText() from span

2,731 views
Skip to first unread message

Prasad KTM

unread,
Oct 7, 2015, 1:59:54 AM10/7/15
to seleniu...@googlegroups.com
I'm trying to extract the string "stealth" from the below HTML code, using Selenium/WebDriver code

String nameXpath ="//div/span[contains(@class,cluster-name-icon-wrapper)]";
WebElement nameElement = driver.findElement(By.xpath(clusterNameXpath));
System.out.println("nameElement TEXT : " + nameElement.getText());

I always get null printed as output
How can I get the desired output for this case?

Note: I understand the HTML code does not meet w3c standards. But currently I have no other choice

Can someone please help


HTML code snippet:
==================
<span class="cluster-name-icon-wrapper header-cluster-name ng-binding">
<i class="fa fa-angle-double-right"></i>
<a ng-click="resetSearch()" ng-href="" style="color:#FFF">
<i class="fa fa-search"></i>
</a>
<i class="fa fa-angle-double-right" ng-hide="cluster_name===''"></i>
 stealth
</span>

Ripon Al Wasim

unread,
Oct 7, 2015, 2:23:09 AM10/7/15
to seleniu...@googlegroups.com
Please try with the following code:

String nameXpath ="//span[contains(@class,cluster-name-icon-wrapper)]";
WebElement nameElement = driver.findElement(By.xpath(nameXpath));
System.out.println("nameElement TEXT : " + nameElement.getText());

I have changed the xpath little bit. I have discarded div at starting in your xpath.


br,
Ripon

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CANpwpiSV3dLjk7iVNau%2BbEChfe6b%2Bbk7RoJ7ACgAgfp-wEOPYA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

http://thebuddhatree.blogspot.in/

unread,
Oct 7, 2015, 3:41:16 AM10/7/15
to Selenium Users
You can also try getAttribute("value") method

Ripon Al Wasim

unread,
Oct 7, 2015, 3:45:30 AM10/7/15
to seleniu...@googlegroups.com
In this case getAttribute("value") method would not work as there is no value attribute in HTML code.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.

Prasad KTM

unread,
Oct 7, 2015, 4:30:42 AM10/7/15
to seleniu...@googlegroups.com
Hi Ripon,

Firstly thanks for taking your time to help me.

From what I see, you have removed the //div, & directly going for span.
I tried the same after seeing your email.

But still no luck.

 

Ripon Al Wasim

unread,
Oct 7, 2015, 4:40:11 AM10/7/15
to seleniu...@googlegroups.com
Hi,

I ran the following code here and it works fine for me:

String nameXpath ="//span[contains(@class, 'cluster-name-icon-wrapper')]";
WebElement nameElement = driver.findElement(By.xpath(nameXpath));
System.out.println("nameElement TEXT : " + nameElement.getText());

The output was: nameElement TEXT : stealth

Please try the above again. You ignore the singe quote in class name also. Please run the above code and let me know.

Ripon

Mark Collin

unread,
Oct 7, 2015, 5:42:36 AM10/7/15
to Selenium Users
Is that XPath finding multiple elements on the page?

You may be getting another element higher up in the DOM that does not have any text associated with it.

Does the span live inside a div?  If not the XPath is wrong.

A CSS locator would be much cleaner in this case:

WebElement myElement = driver.findElement(By.cssSelector(".cluster-name-icon-wrapper"));
System.out.println("myElement Text: " + myElement.getText());

Ripon Al Wasim

unread,
Oct 7, 2015, 5:47:33 AM10/7/15
to seleniu...@googlegroups.com
Yes, that also should work well.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.

Prasad KTM

unread,
Oct 8, 2015, 9:17:14 AM10/8/15
to seleniu...@googlegroups.com
Thanks Ripon.

That works. Awesome!



Prasad KTM

unread,
Oct 8, 2015, 9:17:41 AM10/8/15
to seleniu...@googlegroups.com
Thanks Mark. The css locator too worked well for me.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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