javascript - JavascriptExecutor:使用名称设置属性
问题描述
我想使用的 HTMLJavascriptExecutor
没有id
. 它只包含name
,class
和tag
。其中只有一个name
是唯一的,其余两个对于 DOM 中存在的许多其他 WebElement 是常见的。
我试过了:
String javaScriptCode = "document.getElementsByName('ac118672').setAttribute('value','00031454476543');";
jse().executeScript(javaScriptCode);
这给了我错误
document.getElementsByName(...).setAttribute is not a function
,这很明显,因为按照此处https://www.w3schools.com/jsref/met_doc_getelementsbyname.aspdocument.getElementsByName
的描述收集所有元素。
那么有没有其他方法可以value
使用 JavascriptExecutor 更改属性值?
我拿的参考资料:
解决方案
在尝试了很多方法之后,之前和之后睡觉,它没有工作。
最后对我有用的是:
document.getElementsByName('ac118672')[0].value='00031454476543';
IEjse().executeScript(document.getElementsByName('ac118672')[0].value='00031454476543');
我想setAttribute
也做同样的事情,但它没有用。
我在 Windows 7 上的 Chrome 和 Gecko 驱动程序最新版本上对其进行了测试。
推荐阅读
- php - 我在将数据从 PHP 更新到我的 SQL 数据库时发现了一个问题,我想根据学生 ID 进行更新
- ajax - 重新加载页面后 Ajax 查询不起作用
- php - 在 Woocommerce 中添加产品属性列以编辑订单页面
- html - 网页抓取 excel VBA
- php - 嗨,我想使用电报机器人从电报表中读取参数并将其发送给用户,但我的请求失败
- php - 无法在 packagist 搜索并需要私有作曲家包
- javascript - 从方法中的回调获取主类(NODEJS)
- python - Python:(Pandas)如何忽略按 id 分组的最低和最高 25% 的值以进行均值计算
- ssl - 我遇到此错误:站点缺少有效的受信任证书(net::ERR_CERT_AUTHORITY_INVALID)
- c# - 我正在使用 c# DateTime,我需要以 24 小时格式获取 .Hour 属性