如何编辑HTML(删除readonly)并使用JS Executor键入输入框?

时间:2018-04-30 17:14:20

标签: javascript html selenium-webdriver

这是特定页面上的HTML代码:

<input size="24" autocomplete="off" id="sch-555-et" 
name="./schimage" class="x-form-text x-form-field 
x-trigger-noedit" readonly="" style="width: 263px;" type="text">

它允许浏览图像并禁止用户输入。我被告知跳过浏览部分,因为它不容易出错,也不是手动或自动化测试的一部分。

我知道如何使用JS Executor(https://www.softwaretestingmaterial.com/javascriptexecutor-selenium-webdriver/)键入,但不知道如何编辑HTML然后输入。

修改 我不知道我的问题是如何重复Disable readonly to text box onclicking the button

无论如何我试过了:

public JavascriptExecutor executeJS() {
    JavascriptExecutor jsexec = (JavascriptExecutor).driver;
    return jsexec;
}

executeJS().executeScript("document.getElementById('sch-555-et').removeAttribute('readonly')");

但它并没有删除&#34; readonly&#34;和元素仍然是不可编辑的。

我也试图自动化这个页面http://jsfiddle.net/343Rb/,这里它给出了错误

  1. Cannot set property 'readOnly' of null在尝试将readonly的值设置为null时。
  2. 尝试删除readonly时
  3. Cannot read property 'removeAttribute' of null
  4. 我使用的是Windows,Java,Selenium,Maven和TestNg

1 个答案:

答案 0 :(得分:0)

您可以编辑value属性以模拟输入。即使存在readonly属性,这也将起作用。但是,如果您确实要删除该属性,请使用Element.removeAttribute

var el = document.getElementById('sch-555-et');

// Note: you can edit the value even if the readonly attribute is present.
el.removeAttribute('readonly');

el.value = 'Edited';
<input size="24" autocomplete="off" id="sch-555-et" 
name="./schimage" class="x-form-text x-form-field 
x-trigger-noedit" readonly="" style="width: 263px;" type="text">