【问题标题】:how to set html node value using webview with javafx如何使用带有 javafx 的 webview 设置 html 节点值
【发布时间】: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 的帮助。我的问题解决了。

标签: java javafx webview


【解决方案1】:

我在网上冲浪后找到了解决方案。问题是我使用的是设置节点值,但输入标签的值是使用 HTMLInputElement 设置的。这个链接对我来说很有价值 Performing an automated form post of login using webview 例如

HTMLInputElement password = (HTMLInputElement) nodes.item(0).setValue("yadayada");

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 2013-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2018-01-29
    相关资源
    最近更新 更多