I want to fill two textfields automatically and simulate a buttonclick to automatically log in to a website. I'm using the JavaFX WebView and its function .executeScript()
This is what I've got so far: (Refering to Sergey Grinevs answer on this question: Execute a Javascript function for a WebView from a JavaFX program)
webEngine.load("websiteexample.com");
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
webEngine.executeScript(
"function login(user,pass){"
+ " var usernameField = document.getElementById(\"username\");"
+ " var passwordField = document.getElementById(\"password\");"
+ " usernameField.value = user;"
+ " passwordField.value = pass;"
+ " var sButton = document.getElementById(\"submit\");"
+ " sButton.click();"
+ "}"
+ "login('abc','123');");
}
}
});
So far this works but the Javascript will be executed in an infinite loop. I furthermore dont know why there is a Listener.
Need to say that I've copied my script into the script from Sergey Grinev since that worked for me.
Otherwise, by simply calling executeScript, it will throwjava.lang.reflect.InvocationTargetException
and many more.
How can I execute my script without a permanent loop and why does it not work without the listener?
Thanks for reading and kind regards