Re: getText() from a disabled element (java)

365 views
Skip to first unread message

MartinP

unread,
Sep 10, 2012, 8:45:02 AM9/10/12
to seleniu...@googlegroups.com
Found a solution:  getAttribute("innerHTML") does the job!

Any other/better options for getting the text from disabled elements?




On Monday, September 10, 2012 12:50:02 PM UTC+1, MartinP wrote:
Hi all, I'm sure there must be a simple way to do this, but I've not managed to find it.

I want to get the text out of an element (an h3 heading) that is not currently displayed on the page, but is in the html. 
I've got the WebElement object representing the element (I'm using Java), but calling getText() only returns visible text, and as the entire element is not visible, I get nothing back.

Any thoughts?

Martin.

SantoshSarma

unread,
Sep 10, 2012, 8:59:28 AM9/10/12
to seleniu...@googlegroups.com
Hi Martin..!

Have you tried with getAttribute("value"); ?


Regards,
SantoshSarma

MartinP

unread,
Sep 10, 2012, 9:03:57 AM9/10/12
to seleniu...@googlegroups.com
I did read about getAttribute("value") and gave it a try, but it returned null for me unfortunately.

Peter Gale

unread,
Sep 10, 2012, 9:07:01 AM9/10/12
to Selenium Users
That'll could be because it is disabled.

Why do you need to get the text of a disabled element, Martin?


Date: Mon, 10 Sep 2012 06:03:57 -0700
From: martin.ph...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: getText() from a disabled element (java)
--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/NCBL1jiKjJEJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

MartinP

unread,
Sep 10, 2012, 9:25:32 AM9/10/12
to seleniu...@googlegroups.com
I admit it's a bit of a weird case, but I'm not actually testing the web-page itself, I'm testing the AppServer that displays the web page - the web page being the App Server's self diagnostic page.

The page I'm accessing has about 6 expandable sections in it, each section has a number of Status entries (75 in total at the moment).  I'm checking that all Status entries are 'good', and if any aren't I want to get the content out of them. As all the Status entries load themselves during the page load, and they are all accessible by getting Elements with a specific class, so I can access all Statuses without caring if they are displayed or not, and without having to navigate through the page to get them to be displayed.

e.g.

        List<WebElement> statusElements = driver.getAllByClassName(status);
        List<WebElement> goodElements = driver.getAllByClassName(good);

        //Removing good elements from the list of all statuses
        statusElements.removeAll(goodElements);

        for (WebElement nonGoodElem : statusElements) {
            // The 'heading' for the status is in am h3 within the status div
            String heading = nonGoodElem.findElement(By.xpath(".//h3")).getAttribute("innerHTML");
            log("non-good Status  = " + heading);
        }


<div class="status good">
<h3>PHP</h3>
<p> You have PHP version 5.3.2-1ubuntu4.17.</p>
</div>
<div class="status bad">
<h3>Clean URLs</h3>
<p> Request rewriting does not appear to be working correctly. Please verify your web server's request rewriting configuration and try again. For Apache, ensure the mod_rewrite module is enabled.</p>
</div>
(Fingers crossed the formatting works OK for this!)

Peter Gale

unread,
Sep 10, 2012, 9:41:27 AM9/10/12
to Selenium Users
If it is quicker to check against the disabled elements than enable them separately and then check, and if you're happy that the hidden values you're getting with the innerHTML option are the right ones then it sounds like a reasonable approach and probably as quick a way as you'll get for what you want to do.


Date: Mon, 10 Sep 2012 06:25:32 -0700
From: martin.ph...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: getText() from a disabled element (java)
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Yf_tdkjMuWcJ.

MartinP

unread,
Sep 10, 2012, 9:44:03 AM9/10/12
to seleniu...@googlegroups.com
Many thanks for the confirmation.  Wanted to make sure I wasn't missing something obvious! :-)

David

unread,
Sep 10, 2012, 1:14:33 PM9/10/12
to seleniu...@googlegroups.com
Also, just wanted to mention that if you don't want or need the HTML and just want text, then can also try attribute "innerText".

MartinP

unread,
Sep 11, 2012, 6:15:39 AM9/11/12
to seleniu...@googlegroups.com
That's great David - thanks, that's a much better one for me to use.  Do you know if there a list somewhere of all of these built in 'attributes'?

David

unread,
Sep 11, 2012, 4:42:08 PM9/11/12
to seleniu...@googlegroups.com
I don't know where one might find a comprehensive list, but here's one:


though for some reason it doesn't list innerText. Another option is to search HTMLElement attributes or DOM attributes to see what list you can pull up. And most of these are DOM based attributes and will only be accessible from WebDriver and not Selenium RC, or at least from RC, you have to get them off the browserbot object and not from native RC getAttribute() method.
Reply all
Reply to author
Forward
0 new messages