getAttribute and getText not working

491 views
Skip to first unread message

Hermez Artez

unread,
Apr 23, 2012, 12:27:25 PM4/23/12
to Selenium Users
I have an input field that takes text and looks like this:

<input
id="ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName"
type="text" size="30" maxlength="50" name="ctl00$ctl00$mainContent
$bodyContentPlaceHolder$nameInputControl$txtFirstName">


I have populated this input field with text and then when i try to get
the values i get the following:

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getText()

returns an empty string


driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getAttribute("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName")

returns NULL

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getAttribute("ctl00$ctl00$mainContent
$bodyContentPlaceHolder$nameInputControl$txtFirstName")

returns NULL

Please suggest something that works.

Tarun Kumar

unread,
Apr 24, 2012, 1:29:09 AM4/24/12
to seleniu...@googlegroups.com
After having written the text in the text box, Did you observe the html?
Does it appear as innerText (in that case getText()) or value attribute (in that case getAttribute('value'))?

Also you code about getAttribute does not look right -

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getAttribute("ctl00$ctl00$mainContent 
$bodyContentPlaceHolder$nameInputControl$txtFirstName")  

Herein - "ctl00$ctl00$mainContent $bodyContentPlaceHolder$nameInputControl$txtFirstName" is the name attribute. If you were wanting to get the value of name attribute then you would use it as -

river.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getAttribute("name")  

~tarun

Hermez Artez

unread,
Apr 24, 2012, 11:11:08 AM4/24/12
to Selenium Users
Thanks for responding!


When I enter text into the element with id:
ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName
and then check it with firebug, the HTML is unchanged. It is exactly
the same as it was without text in the text field.

Also when I use:

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName")).getAttribute("name")
-> this returns the ID name, not the value of the text inside the
element.

Therefore it returns:
ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName

Hermez Artez

unread,
Apr 24, 2012, 11:19:15 AM4/24/12
to Selenium Users
Essentially the field that I am trying to get the value from is an
Input field.

I browse to the webpage -> input my first name into the element with
ID:ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName

The HTML is unchanged since it is a form -> only after the page is
submitted are those values sent.

Is there any way that I can get these input values?

Moises Siles

unread,
Apr 24, 2012, 11:19:11 AM4/24/12
to seleniu...@googlegroups.com
could you try something like

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").text

or 

driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_nameInputControl_txtFirstName").getAttribute("value") 


--
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.


Hermez Artez

unread,
Apr 24, 2012, 11:21:21 AM4/24/12
to Selenium Users
driver.findElement(By.id("ctl00_ctl00_mainContent_bodyContentPlaceHolder_
nameInputControl_txtFirstName")).getAttribute("value")

WORKS

Thank You

Moises Siles

unread,
Apr 24, 2012, 11:45:41 AM4/24/12
to seleniu...@googlegroups.com
cool, :)

Tarun Kumar

unread,
Apr 25, 2012, 12:32:32 AM4/25/12
to seleniu...@googlegroups.com
I assume I was not to the point when I said -

<quote>Does it appear as innerText (in that case getText()) or value attribute (in that case getAttribute('value'))?</quote>

Hermez Artez

unread,
Apr 25, 2012, 1:47:26 PM4/25/12
to Selenium Users
At first I was under the impression that getAttribute("value") -> I
had to replace the word "value" with somethign that I was actually
looking for in the html. There was the attribute "name" in the html,
however, there is none called value. I did not realise that using
"value" was actually the correct syntax.
Reply all
Reply to author
Forward
0 new messages