Get only visible text from a page

2,163 views
Skip to first unread message

Joem

unread,
Mar 8, 2012, 12:37:26 PM3/8/12
to Selenium Users
Hi

Is there a way to only get the text that is actually (not just
theoretically) visible in a browser via Selenium2/Java?

I'm currently getting all the text from a page using

driver.findElement(By.cssSelector("body")).getText()

This is generally fine - it gets all apparently visible text. However
I've just noticed that on one page for some unknown reason the text in
one of the h1's is not showing up when viewed in IE8 but is being
shown correctly in FF/Opera/Chrome. However, the text returned by
getText() for IE8 still includes the text from the invisible h1 - so
the returned text is not representative of what a user will actually
see.

Note: If I just get the h1 element in question and run isDisplayed()
on it then it returns true - even though it is not actually visible,
so I can't reliably use this either (i.e. can't get all elements,
check that they're displayed, and only then get their text).

Kanik Sharma

unread,
Mar 8, 2012, 12:54:06 PM3/8/12
to seleniu...@googlegroups.com
there might be some script error on page that might be stopping that H1 text to appear.You might need to analyse that tag.
One similar way is :
driver.findElement(By.tagName("body")).getText();





--
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 this group at http://groups.google.com/group/selenium-users?hl=en.


Afzal Hussain Shaik

unread,
Mar 8, 2012, 3:13:26 PM3/8/12
to seleniu...@googlegroups.com
HI 

This one you can achieve through using method called "getVisibleText()"

Regards.
Afzal

--
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 this group at http://groups.google.com/group/selenium-users?hl=en.




--
Thanks & Regards,
Afzal Hussain Shaik


Joem

unread,
Mar 8, 2012, 5:53:47 PM3/8/12
to Selenium Users
Hi there

Thanks for the replies. I tried using tagName but the result was the
same unfortunately. And it doesn't seem like getVisibleText is
available in webdriver. Might investigate jquery.

Regards

Joe

On Mar 8, 8:13 pm, Afzal Hussain Shaik <shaik.afzalhuss...@gmail.com>
wrote:
> *Thanks & Regards,
> Afzal Hussain Shaik*
> *
> *
Reply all
Reply to author
Forward
0 new messages