Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8292046

WebView engine not handling all DOM elements

XMLWordPrintable

    • web
    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      PC / Windows 10 / Java 18 / JavaFX 18.0.2

      A DESCRIPTION OF THE PROBLEM :
      Using a WebView to connect to my company employees webpage, the login process is handled properly (it's an SSO login page) BUT when I try to retrieve the input fields (in a documentProperty Listerner) via webView.getEngine().getElementsByTagName("input"), only two hidden fields are return and not the active login and password ones. As a matter of a fact, it's a whole bunch of elements that are not present in the engine's DOM despite they are properly rendered and usable.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - open a webview and load the following url : <link>
      - in a document property listener, get the newDoc call getElementsByTagName("input") and return the result in a NodeList
      - display the attributes of all returned nodes

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      One should have 16 input elements returned, as when you do the call in firefox console : console.log(document.getElementsByTagName('input'));
      ACTUAL -
      Only 2 input elements are returned : the ones named _xsrfToken and fromURI

      ---------- BEGIN SOURCE ----------
      /*
       * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
       * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMLController.java to edit this template
       */


      import java.net.CookieHandler;
      import java.net.CookieManager;
      import java.net.CookiePolicy;
      import java.net.HttpCookie;
      import javafx.fxml.FXML;
      import javafx.scene.web.WebEngine;
      import javafx.scene.web.WebErrorEvent;
      import javafx.scene.web.WebEvent;
      import javafx.scene.web.WebView;
      import javafx.stage.Stage;
      import org.w3c.dom.Attr;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.NamedNodeMap;
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;


      public class LoginViewController {

          @FXML
          private WebView webView;
          
          private WebEngine we;
          CookieManager cookieManager;
          Stage loginStage;
          MainViewController mainViewController;

          @FXML
          public void initialize() {
              we = webView.getEngine();
              cookieManager = new CookieManager();
              cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
              CookieHandler.setDefault(cookieManager);
              connect();
          }

          /**
           * Sets the stage of this dialog.
           *
           * @param dialogStage
           */
          public void setStage(Stage loginStage) {
              this.loginStage = loginStage;
          }

          public void setMainViewController(MainViewController mainViewController) {
              this.mainViewController = mainViewController;
          }

          public void connect() {
              we.documentProperty().addListener((obs, oldDoc, newDoc) -> {
                  if (newDoc != null) {
                      listNodesByName(newDoc, "input");
                  }
              });

              we.locationProperty().addListener((obs, oldLocation, newLocation) -> {
                  if (newLocation.equals("<link>")) {
                      for (HttpCookie cookie : cookieManager.getCookieStore().getCookies()) {
                          if (cookie.getName().contains("SessionId")) {
                              mainViewController.setSessionId(
                                      cookie.getName() + "=" + cookie.getValue());
                          }
                      }
                      mainViewController.launchConnectionWithToken();
                      loginStage.close();
                  }
              }
              );

              we.setJavaScriptEnabled(true);
              we.setOnAlert(
                      (WebEvent<String> event) -> {
                          System.out.println(event.toString());
                      }
              );
              we.setOnError(
                      (WebErrorEvent event) -> {
                          System.out.println(event.toString());
                      }
              );
              we.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0");
              we.load("<link>");
          }

          private void listNodesByName(Document doc, String name) {
              NodeList nl = doc.getElementsByTagName(name);
              if (nl != null) {
                  System.out.println(nl.toString());
                  for (int i = 0; i < nl.getLength(); i++) {
                      if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                          Element el = (Element) nl.item(i);
                          listAllAttributes(el);
                      }
                  }
              }
          }

          private void listAllAttributes(Element element) {

              System.out.println("List attributes for node: " + element.getNodeName());

              // get a map containing the attributes of this node
              NamedNodeMap attributes = element.getAttributes();

              // get the number of nodes in this map
              int numAttrs = attributes.getLength();

              for (int i = 0; i < numAttrs; i++) {
                  Attr attr = (Attr) attributes.item(i);

                  String attrName = attr.getNodeName();
                  String attrValue = attr.getNodeValue();

                  System.out.println("Found attribute: " + attrName + " with value: " + attrValue);
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: