Java 删除 Selenium WebDriver 中的只读属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22711441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:25:21  来源:igfitidea点击:

Remove readonly attributes in Selenium WebDriver

javajavascriptfirefoxseleniumselenium-webdriver

提问by aurbano

I need to edit some readonlyfields 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 readonlyattribute using a JavaScript snippet with the JavaScript Executor.

我需要readonly在 Java 中使用 Selenium WebDriver编辑一些字段。由于 Selenium 甚至不会让我找到这些字段,因此我搜索了解决方案,发现最简单的方法可能是readonly使用 JavaScript 代码段和JavaScript Executor删除该属性。

While this snippet works from the Firefox console, successfully removing the attribute from all inputs, it throws an exception in Selenium.

虽然此代码段在 Firefox 控制台中工作,成功地从所有输入中删除了该属性,但它会在 Selenium 中引发异常。

JavaScript executor:

JavaScript 执行器:

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

线程“main”org.openqa.selenium.WebDriverException 中的异常:非法字符

命令持续时间或超时:51 毫秒

UPDATE:

更新:

The same error appears if I leave only the first JS command:

如果我只留下第一个 JS 命令,则会出现相同的错误:

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

堆栈跟踪的其余部分与此无关。任何人都知道如何解决这个问题,或其他编辑readonly字段的方法?

采纳答案by Sighil

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.

如果这对您有帮助,请告诉我。

回答by ddavison

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.

我修复了它只是将它放在一行中,但是我仍然建议使用@lost 的答案,因为它更明确。

@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 hereand the page i used to test here

看到剧本在这里,我用来测试的页面在这里

回答by Vasudha

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

这对我有用