最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Remove readonly attributes in Selenium WebDriver - Stack Overflow

programmeradmin4浏览0评论

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 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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 28, 2014 at 11:20 Alejandro Urbano AlvarezAlejandro Urbano Alvarez 3,3461 gold badge26 silver badges39 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

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.

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

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)

  1. 暂无评论