How can we verify whether a field is write-protected (that is, readonly) in Selenium using Java code?
Best regards
Use like this:
WebElement some_element = driver.findElement(By.id("some_id"));
String readonly = some_element.getAttribute("readonly");
Assert.assertNotNull(readonly);
Do NOT verify getAttribute("readonly").equals("true") or similar, in different browsers it can be different as well. (readonly="readonly" in IE, readonly="" in FF, etc.)
Since I don't know for what you need that check i'll post some examples that might be usefull.
driver.findElements(By.cssSelector("input:not([readonly='readonly'])[type='text']"));
=> returns all text input fields that are editable
WebElement element = driver.findElement(By.id("username");
//can fail if the attribute is not there
String attribute = element.getAttribute("readonly");
=> might need a try catch block
You can try to write something via sendkeys() and check that value attribute of textbox has not been changed.
The WebElement
interface has a function called isEnabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With