Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove readonly attributes in Selenium WebDriver

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?

like image 927
aurbano Avatar asked Dec 04 '22 07:12

aurbano


1 Answers

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.

like image 67
Sighil Avatar answered Dec 14 '22 12:12

Sighil