Search code examples
c#seleniumselenium-webdrivernunitassert

Asserting value for read-only field using Selenium webdriver C#


I am using Selenium webdriver with NUnit framework and I can't get around to Assert a read-only text field on a html form. Selenium is able to find the element by 'Id' but unable to read the text within the element and is returning a blank string, and my assertion fails.

Below is the block of code on the application -

<td nowrap="" colspan="2">
<input readonly="" tabindex="-1" class="readonly" type="text" 
onfocus="gcurrObj=this;JS_FieldFocus(this, 'frmDetails')" 
onchange="JS_FieldChange(this, 'frmDetails')" id="txtAccountName" 
name="undefined" value="James Bond" size="60" 
style=""></td>

I am using the following code to read, but selenium is returning a blank string

var AccName = driver.FindElement(By.Id("txtAccountName"));
Assert.That(AccName.Text.Contains("James"));

Any help is appreciated..!!


Solution

  • The text in the text fields is not part of the WebElement, you can't get it with the Text property. To get it you need to use the value attribute

    Assert.That(AccName.GetAttribute("value").Contains("James"));