我想使用 JavascriptExecutor
操作的 HTML 没有 id
。它只包含name
、class
和tag
。其中只有 name
是唯一的,其余两个对于 DOM 中存在的许多其他 WebElements 是常见的。
我试过了:
String javaScriptCode = "document.getElementsByName('ac118672').setAttribute('value','00031454476543');";
jse().executeScript(javaScriptCode);
这给了我错误
document.getElementsByName(...).setAttribute 不是函数
,这是显而易见的,因为 document.getElementsByName
给出了此处描述的所有元素的集合 https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp .
那么有没有其他方法可以使用 JavascriptExecutor 更改 value
属性的值?
引用文献:
How to edit HTML (remove readonly) and type in input box using JS Executor?
最佳答案
试了很多方法,前后 sleep ,都不行。
最后对我有用的是:
document.getElementsByName('ac118672')[0].value='00031454476543';
即jse().executeScript(document.getElementsByName('ac118672')[0].value='00031454476543');
我想 setAttribute
也做了同样的事情,但它没有用。
我在 Windows 7 上的 Chrome 和 Gecko Driver 最新版本上测试了它。
关于Javascript执行器 : setAttribute using name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53527626/