3

I am trying to login to a website and then load a url. This is what I have so far

package application;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import org.w3c.dom.*;
import org.w3c.dom.html.*;

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

    @Override
    public void start(Stage stage) {

        final BooleanProperty loginAttempted = new SimpleBooleanProperty(false);

        final WebView webView = new WebView();
        webView.setPrefWidth(1000);

       final WebEngine engine = webView.getEngine();
       engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override
            public void changed(ObservableValue<? extends Document> ov, Document oldDoc, Document doc) {
                if (doc != null && !loginAttempted.get()) {
                    //Submits Form 
                }
            } 
        });
        engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
            @Override
            public void changed(ObservableValue<? extends Throwable> ov, Throwable oldException, Throwable exception) {
                System.out.println("Load Exception: " + exception);
            }
        });

        Button fxLoginButton = new Button("Login");
        fxLoginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (notEmpty(fxPassword.getText()) && notEmpty(fxPassword.getText())) {
                    loginAttempted.set(false);
                    engine.load("website.com");               
                }
            }
        });
        fxLoginButton.setDefaultButton(true);
        ProgressIndicator fxLoadProgress = new ProgressIndicator(0);
        fxLoadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().progressProperty());
        fxLoadProgress.visibleProperty().bind(webView.getEngine().getLoadWorker().runningProperty());

        HBox loginPane = new HBox(10);
        loginPane.getChildren().setAll(
                fxLoginButton,
                fxLoadProgress
        );


        final VBox layout = new VBox(5);
        layout.setStyle("-fx-background-color: lightgray; -fx-padding: 3;");
        layout.getChildren().addAll(
                loginPane,
                webView
        );

        VBox.setVgrow(webView, Priority.ALWAYS);

        stage.setScene(new Scene(layout));
        stage.show();

        fxUsername.requestFocus();

    }    
}

So basically what happens is that it loads a website(website.com) enters in username and password and then submits the form thus logging in. After it signs in I want to keep visiting the website until the website return Success. So in other words, after logging in load website if it does not return Success load the website again. But in the process I may be signed out how can I then sign in again

Mus Akhi
  • 31
  • 1

0 Answers0