DEnumber50 DEnumber50 - 1 year ago 86
C# Question

Selenium C#: Assert verifying if text field is uneditable

I am trying to write a script to assert if the text field in question isn't editable. I was attempting to write a verification hidden = "true", because that is what ultimately is hiding the editable text box. How would I be able to verify something like this? and is this the way I should be handling such a verification? Thanks for your responses in advance.

Here is the html

<div id="ctl00_contentMain_sponsorInfo">
Referral Information
The name of your referrer is Nameā€¦
<input id="ctl00_contentMain_sponsorUsername" class="textBig" type="text" hidden="true" style="width:270px;" req="false" max="30" value="Name" name="ctl00$contentMain$sponsorUsername"></input>

Answer Source

As you mentioned the best way to test this scenario is to see if the hidden attribute has a value true

IWebElement element = Driver.FindElement(By.Name("ctl00$contentMain$sponsorUsername"));
if (element.GetAttribute("hidden").Contains("true"))
    Assert.Pass("Your message");

Note: You do have to make sure the element exist. You can use some explicit wait with something like

var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("something ")));

in order to grab the attribute. This case Selenium will work just fine since you are not interacting with the element.