Basil Basil - 8 days ago 7
HTML Question

getText() on input is not returning text

I have a confusion regarding getText() in selenium. below given is the web page snippet.

<input checked name=servClass type=radio
value="Coach">
<font face="Arial, Helvetica, sans-serif">Economy class <br>
<input
name=servClass type=radio value="Business">
Business class <br>
<input
name=servClass type=radio value="First">
First class</font></font></td>
</tr>


I am trying to print the radio button text using the below snippet.

List<WebElement> list1 = driver.findElements(By.name("servClass"));
list1.get(1).click();
for (int i=0;i<=2;i++)
{
System.out.println(list1.get(i).getText());
}


This code doesn't print the texts. Could anyone suggest whats wrong in this?

Answer

getText() returns the innerText held by the element

<div id='someid'>
someText
</div>

But input tag can not hold anything. It is not designed for that purpose. It is called void element.

Input does NOT work this way to use getText()

<input id='someid'> text </input>

As input holds the text in the value attribute,you need to use getAttribute("value")

<input id='someid' value='text'/>

Comments