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