Basil Basil - 1 month ago 28
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
<font face="Arial, Helvetica, sans-serif">Economy class <br>
name=servClass type=radio value="Business">
Business class <br>
name=servClass type=radio value="First">
First class</font></font></td>

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

List<WebElement> list1 = driver.findElements("servClass"));
for (int i=0;i<=2;i++)

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


getText() returns the innerText held by the element

<div id='someid'>

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'/>