Selenium web driver test whether field is editable.

3,887 views
Skip to first unread message

Tony Chamberlain

unread,
May 6, 2014, 8:09:03 AM5/6/14
to seleniu...@googlegroups.com
Hi

I am using Selenium with Java.  I need to see whether a field is editable (i.e., you can/cannot type into it).  I did a search and it says you can find the element and use element.isEnabled().  I do use this and the answer is TRUE.  However, I still cannot type into the box.  Something can be enabled but still not editable (developers have confirmed this.  It needs to be enabled for some reason but not editable).

Another site said you could use getAttribute("read-only") but the result will vary depending on browser.  I could have a big if ... but I don't know all the values.

Lacking any other option, I did the following:

public String verifyElementIsReadOnly( String xpathKey ) { // Tony 
String xpath = null;

System.out.println(( "Enter verifyElementIsReadOnly called with xpathKey=" + xpathKey));

try {
Thread.sleep(1000);
xpath = OPS.getProperty(xpathKey);
System.out.println( "verifyElementIsReadOnly: " + xpathKey + "=" + xpath);

// This will cause an exception if  there is an error.
WebElement ele = driver.findElement(By.xpath(OPS.getProperty(xpathKey)));

// If it can be cleared then it is not read-only
ele.clear();

System.out.println( "element " + xpathKey + " is NOT read-only" );;
return "Fail";

} catch ( Exception e ) {
String msg = e.getMessage();
if ( msg.contains("Element is read-only" ) ) {
System.out.println( "element " + xpathKey + " is read-only");
return "Pass";
}

// Some other kind of error
System.out.println( "verifyElementIsReadOnly caught exception " + msg);
return "Fail";
}
}


The problem with this is ele.clear() succeeds, the field will be erased.  This is probably OK since this causes the script to fail, but people might want to see what was in the field.

Any suggestions?

Thanks

David

unread,
May 6, 2014, 2:15:52 PM5/6/14
to seleniu...@googlegroups.com
If you must clear the field to validate read-only, etc. you could store the value in the field prior to clearing it, then if the clear succeeds, you simply sendKeys the value back to it (or use javascript to directly set the value back if not sendKeys).

For form fields, it will typically be the value attribute of the element (getAttribute() against value), but in other case like textarea fields and others it can be the text within the element (getText() ).

Devu

unread,
May 6, 2014, 2:16:14 PM5/6/14
to seleniu...@googlegroups.com
Hi Tony,

I'm not a java expert, here is my experience with read only field. If you are trying to type in something to the read only field, do a click element first , then type in (input) your text.

For example, if i want to type in something in the below xpath I would do the following:

Click Element xpath=//form/div/dl/dd[1]/input
Input Text xpath=//form/div/dl/dd[1]/input {TOP}


Hope that helps!

Devu
Reply all
Reply to author
Forward
0 new messages