我试图在加载到 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");
}
}
}
}
}
最佳答案
我在网上冲浪后找到了解决方案。问题是我正在使用设置节点值,但输入标记的值是使用 HTMLInputElement 设置的。此链接对我来说很有值(value) Performing an automated form post of login using webview 例如
HTMLInputElement password = (HTMLInputElement) nodes.item(0).setValue("yadayada");
关于java - 如何使用带有javafx的webview设置html节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997956/