How to get hidden text of a WebElement?

8,200 views
Skip to first unread message

William S

unread,
Jan 13, 2010, 6:15:28 PM1/13/10
to webdriver
I am trying to get some text which is of a hidden WebElement and I
cannot get any text back. So I debugged source code and see this block
of code in HtmlUnitWebElement:

if (child instanceof DomText) {
if (child.isDisplayed()) {
String textToAdd = ((DomText) child).getData();
textToAdd = textToAdd.replace(nbspChar, ' ');
textSoFar.append(textToAdd);
}
continue;
}

So it on purposely skip the text if it's not being displayed on the
page. My question is that what if I want to get this text? Thanks!

Daniel Wagner-Hall

unread,
Jan 14, 2010, 6:12:17 AM1/14/10
to webd...@googlegroups.com
String innerText = ((JavascriptExecutor)driver).executeScript("return
arguments[0].innerText", element);

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>
>
>

William S

unread,
Jan 14, 2010, 1:55:30 PM1/14/10
to webdriver
Thank you!

On Jan 14, 3:12 am, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> String innerText = ((JavascriptExecutor)driver).executeScript("return
> arguments[0].innerText", element);
>

Adam Scott

unread,
Jan 18, 2010, 11:31:05 AM1/18/10
to webdriver
Out of interest, why is it not possible to read text from hidden
elements without resorting to executing javascript?

If I recall correctly, this used to be possible.

On Jan 14, 6:55 pm, William S <sars...@gmail.com> wrote:
> Thank you!
>
> On Jan 14, 3:12 am, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
>
>
>
> > String innerText = ((JavascriptExecutor)driver).executeScript("return
> > arguments[0].innerText", element);
>
> > On Wed, Jan 13, 2010 at 11:15 PM, William S <sars...@gmail.com> wrote:

> > > I am trying to get some text which is of ahiddenWebElement and I

Jason Leyba

unread,
Jan 18, 2010, 1:47:16 PM1/18/10
to webd...@googlegroups.com
We try not to let you do things with WebDriver that a user couldn't
do. For instance, if a user can't see a button, they can't click it,
so neither can WebDriver. Likewise, a user wouldn't be able to see
the text of a hidden element, so WebDriver will return an empty
string.

-- Jason

William S

unread,
Jan 22, 2010, 8:27:19 AM1/22/10
to webdriver
I am not sure what's WebDriver's main goal. Sorry, I didn't read the
documents. Just want to try out new libs. but if it's to support unit
test like selenium does, then this should be arguable. Because it's
quite often developer will pass values to hidden field in forms and
they want to verify them. I will say it's much nicer if there is a
alternative API findElement(By, boolean hidden). After all, sending
commands directly through javascript is kinda hacking. Another thing
probably nice to have is to get a WebElement's html source. Anyway, I
could be totally wrong, if WebDriver's goal is just to simulate pure
user experiences.

On Jan 18, 10:47 am, Jason Leyba <jmle...@gmail.com> wrote:
> We try not to let you do things with WebDriver that a user couldn't
> do.  For instance, if a user can't see a button, they can't click it,
> so neither can WebDriver.  Likewise, a user wouldn't be able to see
> the text of a hidden element, so WebDriver will return an empty
> string.
>
> -- Jason
>

Anthony Long

unread,
Jan 22, 2010, 4:26:29 PM1/22/10
to webdriver
if a field is hidden it wouldnt be user editable.

you can test the value changes by editing whatever you trigger the
change (from another element that is editable or not hidden) then
confirming with javascript or such.

Simon Stewart

unread,
Jan 26, 2010, 4:20:56 AM1/26/10
to webd...@googlegroups.com
WebDriver doesn't prevent you from reading attributes from a hidden
element (eg: element.getAttribute("foo")), or from reading its value,
even if the element is hidden.

The restriction we place is that if a user would perform the action
you're requesting with a keyboard or mouse (that is, things like
clicking, selecting or typing) then the element must be visible
(otherwise how would the user perform the same actions) This means
that the tests you write not only exercise the raw HTML of the page,
but also verify that a user could use your site the way your tests say
they should.

Regards,

Simon

Latha

unread,
Jun 27, 2013, 2:22:56 PM6/27/13
to webd...@googlegroups.com
Same problem with me,I am unable to get the value of hidden element, used 'getAttribute' as suggested by Simon in the below post...
 
I have only one webelement on the page and all other elements are inside the frames....
 <input type="hidden" name="mylogin" id="mylogin" value="1"/>
 
I am trying to get the value of this webelement to check successful login ....and seeing 'Unable to locate element:' error...
 
Here is what I tried:


String P =driver.findElement(By.name("mylogin")).getText();
driver.findElement(By.name("mylogin")).getAttribute("value");
driver.findElement(By.xpath("//input[@id='mylogin']")).getAttribute("value");

Thanks,

Latha.

 

 

 

darrell

unread,
Jun 30, 2013, 5:52:21 PM6/30/13
to webd...@googlegroups.com
Your problem has nothing to do with the original post of this thread. Locating an element does not require it to be visible. It is only if you try to getText, click etc. I would suggest you start a new thread because you have a new problem.

da...@paradise.net.nz

unread,
Jul 11, 2013, 7:19:08 PM7/11/13
to webd...@googlegroups.com

>We try not to let you do things with WebDriver that a user couldn't
>do.  For instance, if a user can't see a button, they can't click it,
>so neither can WebDriver.  Likewise, a user wouldn't be able to see
>the text of a hidden element, so WebDriver will return an empty
>string.

It’s quite common to have to set a hidden field for select lists where the page submits an ID rather the name in the select list. Normal user interaction with the field triggers the ID to be set, whereas watir or web-driver does not. I get the ID from the DB, set it and can submit.

I have struck this in several web applications in 2 organizations.

For web-driver I have resort to a hack that depends on the field name:

Convert to a text field then set as a text field:

(Ruby)
browser.execute_script("document.getElementsByName('#{field_name}')[0].setAttribute('type',
'text');")
       browser.text_field(:id, field_name).set value

Reply all
Reply to author
Forward
0 new messages