Raggy
Raggy

Reputation: 37

How to check text in an input field

I am newbie to Selenium, and been having trouble verifying the text saved in a field.

This is the html for one of the rows.

<tr id="tier_no_1">
    <td class="ui-styled-table-odd-row"></td>
    <td class="ui-styled-table-odd-row"></td>
    <input id="tier_name" class="tierName required" type="text" name="tier_name"></input>
</tr>

I need to verify the text that is saved/entered into the field. Normally I would use the following, but this just returns a value of 'null'

Assert.assertEquals("Tier 1", findElement(By.cssSelector("#tier_no_1 > td.ui-styled-table-odd-row > #tier_name")).getAttribute("tier_name"));

Upvotes: 0

Views: 1363

Answers (1)

Yi Zeng
Yi Zeng

Reputation: 32865

Try getAttribute("value") to get the content of an input. Make sure the locator is correct.

Assert.assertEquals("Tier 1", driver.findElement(By.cssSelector("#tier_name")).getAttribute("value"));

By the way, id should be unique in HTML, so you just need By.cssSelector("#tier_name").

Upvotes: 4

Related Questions