如何使用JS执行器编辑HTML(删除只读)并在输入框中输入?



这是特定页面上的一段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执行器(https://www.softwaretestingmaterial.com/javascriptexecutor-selenium-webdriver/(键入,但不知道如何编辑HTML,然后键入。

编辑:我不知道我的问题是如何重复的 禁用只读到文本框 点击按钮

无论如何,我试过:

public JavascriptExecutor executeJS() {
JavascriptExecutor jsexec = (JavascriptExecutor).driver;
return jsexec;
}
executeJS().executeScript("document.getElementById('sch-555-et').removeAttribute('readonly')");

但它没有删除"只读",元素仍然不可编辑。

我也试图自动执行此页面 http://jsfiddle.net/343Rb/,但这里给出了错误

  1. 尝试将只读值设置为 null 时Cannot set property 'readOnly' of null
  2. 尝试删除只读时Cannot read property 'removeAttribute' of null

我正在使用Windows,Java,Selenium,Maven和TestNg

您可以编辑value属性以模拟键入。即使存在只读属性,这也将起作用。但是,如果您确实要删除该属性,请使用 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">

最新更新