I need to edit some readonly
fields with Selenium WebDriver in Java. As Selenium won't let me even find this fields I searched for solutions and found that the easiest might be to remove the readonly
attribute using a JavaScript snippet with the JavaScript Executor.
While this snippet works from the Firefox console, successfully removing the attribute from all inputs, it throws an exception in Selenium.
JavaScript executor:
((JavascriptExecutor) driver).executeScript(
"var inputs = document.getElementsByTagName('input');"+
"for(var i = 0; i < inputs.length; i++)"+
"inputs[i].removeAttribute('readonly','readonly');"
);
And the error returned:
Exception in thread "main" org.openqa.selenium.WebDriverException: illegal character
Command duration or timeout: 51 milliseconds
UPDATE:
The same error appears if I leave only the first JS command:
((JavascriptExecutor) driver).executeScript(
"var inputs = document.getElementsByTagName('input');");
The rest of the stack trace is not relevant for this. Anyone knows how to fix this, or another way to edit the readonly
fields?
I was not able to find the issue with your code. But in the meantime use the code given below.
List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
((JavascriptExecutor) driver).executeScript(
"arguments[0].removeAttribute('readonly','readonly')",input);
}
Let me know if this helps you.
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