Remove readonly attributes in Selenium WebDriver

16,673

Solution 1

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.

Solution 2

Apparently there was a very funky character being put into your string.. as I was using my <- and -> arrow keys, it was getting caught for three characters at the end, and in the middle of the string. Seems to be some copy-pasta issue.

I fixed it just be putting it one one line, however I would still recommend going with @lost's answer as it's more explicit.

@Config(url="https://rawgithub.com/ddavison/so-tests/master/22711441.html", browser= Browser.CHROME)
public class _22711441 extends AutomationTest {
    @Test
    public void test() {
        ((JavascriptExecutor) driver).executeScript(
        // the issue was happening                          \/ here and                                                                             \/ here
        "var inputs = document.getElementsByTagName('input');for(var i = 0; i < inputs.length; i++){inputs[i].removeAttribute('readonly','readonly');}"
        );

        setText(By.id("1"), "something")
        .validateText(By.id("1"), "something");
    }
}

See the script here and the page i used to test here

Solution 3

WebElement elementName = driver.findElement(By.xpath("//div[@arid='7']//input[@id='arid7']"));
((JavascriptExecutor) driver).executeScript("arguments[0].removeAttribute('readonly','readonly')", elementName);

This worked for me

Share:
16,673
aurbano
Author by

aurbano

Engineer. I do full-stack development, dev management, devops, and appsec.

Updated on June 09, 2022

Comments

  • aurbano
    aurbano almost 2 years

    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?

  • aurbano
    aurbano about 10 years
    It works great! I'm completely unsure about the reason why it might not have worked before, but after all I've run into so many similar problems that "should" work in Selenium that I am no longer surprised... Thanks
  • Sighil
    Sighil about 10 years
    No problem. We'll try to find out what is wrong with your code.
  • ddavison
    ddavison about 10 years
    see my answer :) some characters (i think they were just NULL characters) were being placed in your string.
  • aurbano
    aurbano about 10 years
    Thanks! I was thinking it had to be something like that, but since i was testing with the Firefox console with no errors I just couldn't figure out the problem.
  • Amanda Cavallaro
    Amanda Cavallaro over 7 years
    Can I get a solution to this in Ruby?