Remove readonly attributes in Selenium WebDriver

前端 未结 3 1606
北恋
北恋 2021-01-13 15:21

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

相关标签:
3条回答
  • 2021-01-13 15:46
    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

    0 讨论(0)
  • 2021-01-13 15:47

    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

    0 讨论(0)
  • 2021-01-13 15:48

    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.

    0 讨论(0)
提交回复
热议问题