I aim for automatically logging into Google Calendar
through a WebEngine
in JavaFX
and show it in a WebView
. But I can't seem to insert a string value into the password field even though it works perfectly with the username field.
I have tried creating the script in Java with Element e = webEngine.getDocument().getElementById(password)
and inserting the value with e.setAttribute("value","mypassword")
, but this doesn't work either. It provides no stack traces meaning that the element exists, but I can't insert the value somehow.
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
WebEngine webEngine = webView.getEngine();
webEngine.load("https://calendar.google.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){"
+ " var usernameField = document.getElementById(\"identifierId\");"
+ " usernameField.value = user;"
+ " var sButton = document.getElementById(\"identifierNext\");"
+ " sButton.click();"
+ "}"
+ "login('myusername');"
);
}
}
});
Thread t = new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater( () -> {
webEngine.executeScript(
"function login(pass){"
+ " var passwordField = document.getElementById(\"password\");"
+ " passwordField.value = pass;"
+ " var sButton = document.getElementById(\"passwordNext\");"
+ " sButton.click();"
+ "}"
+ "login('mypassword');"
);
});
});
t.start();
The result should be, that the listener waits for the page to load and then fill the username and click next. Then the thread begins but waits 5 seconds to make sure that next has been clicked and then it should fill in the password credentials but doesn't.