删除Selenium WebDriver中的只读属性 [英] Remove readonly attributes in Selenium WebDriver

查看:2640
本文介绍了删除Selenium WebDriver中的只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java中的Selenium WebDriver编辑一些 readonly 字段。由于Selenium不会让我发现这个字段,所以我搜索了解决方案,发现最简单的方法是使用一个JavaScript代码片段来移除 readonly 属性,其中 JavaScript Executor



这段代码从Firefox控制台运行,成功从所有输入中删除属性,在Selenium中抛出一个异常。


$ b JavaScript执行程序: / b>

 ((JavascriptExecutor)驱动程序).executeScript(
var inputs = document.getElementsByTagName('input'); +
for(var i = 0; i< inputs.length; i ++)+
inputs [i] .removeAttribute('readonly','readonly');
);

并返回错误:


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

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

更新

如果我只留下第一个JS命令:
$ b $ pre $ ((JavascriptExecutor)驱动程序).executeScript(
var inputs = document.getElementsByTagName('输入'););






堆栈跟踪的其余部分与这个。任何人都知道如何解决这个问题,或者另一种方式来编辑 readonly 字段?

解决方案

我无法找到您的代码的问题。但同时使用下面给出的代码。

  List< WebElement> inputs = driver.findElements(By.tagName(input)); (WebElement input:inputs){
((JavascriptExecutor)driver).executeScript(
arguments [0] .removeAttribute('readonly','readonly')

,输入);



$ b $ p
$ b

让我知道这是否对您有帮助。


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

这篇关于删除Selenium WebDriver中的只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆