【发布时间】:2017-03-24 11:08:55
【问题描述】:
我正在尝试在加载到 webview 后设置 html 表单元素的值。我尝试使用
进行设置org.w3c.dom.Document doc = webEngine.getDocument();
HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
NodeList nodes = form.getElementsByTagName("input");
nodes.item(1).setNodeValue("yadayada"); //this is where i am setting the value
但没有成功。谁能帮帮我。这是我的代码。
org.w3c.dom.Document doc = webEngine.getDocument();
if (doc!=null && doc.getElementsByTagName("form").getLength() > 0) {
HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
String username = null;
String password = null;
NodeList nodes = form.getElementsByTagName("input");
for (int i = 0; i < nodes.getLength(); i++) {
if(nodes.item(i).hasAttributes()){
NamedNodeMap attr = nodes.item(i).getAttributes();
for (int j=0 ; j<attr.getLength();j++){
Attr atribute = (Attr)attr.item(j);
if(atribute.getValue().equals("password")){
System.out.println("Password detected");
nodes.item(i).setNodeValue("123456");
}
}
}
}
}
【问题讨论】:
-
您能发布一个应该使用的示例 HTML 文件吗? (更好的是,创建一个minimal reproducible example。)
-
感谢@James_D 的帮助。我的问题解决了。