0

I am attempting to load a web page with the JavaFX WebView control. The specific page is: https://bitfinex.com/login. Then, once the load worker's state has been set to SUCCEEDED, I use the w3c Document API to execute Javascript on the loaded webpage. Specifically I want to auto-fill the email login input and then focus the password input. For some reason, for this particular page, nothing happens (the page just loads and the two text inputs remain blank). I have used this exact procedure on a couple other login pages, and it has worked flawlessly (one example being: https://www.coinbase.com/signin). This leaves me very confused. My thoughts have ranged from is the Bitfinex site somehow "blocking" interaction? Also, why is the load worker state set to SUCCEEDED twice (you will see I use an if statement to guard against this by checking if the two elements are still null, which should not be necessary to my understanding and wasn't necessary for the other pages, but I don't know if this is related to the problem at hand? Is this some really obscure, strange bug that will be very difficult to replicate/drill-down? Anyways..I hope this will be cleared up.

The code:

package test;

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import org.w3c.dom.Document;
import org.w3c.dom.html.HTMLInputElement;

public class MainApp extends Application
{
    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    public void start(Stage stage) throws Exception
    {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load("https://www.bitfinex.com/login");
        webView.setPrefSize(610, 610);
        webEngine.getLoadWorker().stateProperty().addListener((observableValue, oldState, newState) ->
        {
            if (newState == Worker.State.SUCCEEDED)
            {
                Document document = webEngine.getDocument();
                if (document.getElementById("login") == null || document.getElementById("auth-password") == null)
                {
                    return;
                }
                ((HTMLInputElement) document.getElementById("login")).setValue("john_appleseed@gmail.com");
                ((HTMLInputElement) document.getElementById("auth-password")).focus();
            }
        });
        stage.setScene(new Scene(webView));
        stage.show();
    }
}
brcolow
  • 1,042
  • 2
  • 11
  • 33
  • What do you get on the console, if you inject [firebug](http://stackoverflow.com/questions/9398879/html-javascript-debugging-in-javafx-webview)? Also take a look at the code of the page: When loading there are many requests and there is JavaScript heavily involved. – hotzst Jan 19 '16 at 11:59
  • Cool suggestion! Using the Firebug console, attempting to execute `document.getElementById('login').value='Hello'` does nothing as well. What does this mean? – brcolow Jan 19 '16 at 19:23
  • I guess that at the time the state switches to `SUCCEDED` the web pages JavaScript has not yet completely initialized and the elements you are looking for are not yet present in the DOM. When I have to interact with a web page from javafx, I register a Java<->JavaScript bridge and do the work in JavaScript. – hotzst Jan 20 '16 at 06:28
  • I opened a Firebug console as you suggested and actually tried executing the command every one minute for 20 minutes and it still doesn't do anything, so I am not sure if that's the case. Anyways, I am going to go with a different approach of using the same API that their javascript uses, but from Java. – brcolow Jan 20 '16 at 20:33

0 Answers0