0

PROBLEM: Click a button on a loaded page (via JavaFX WebEngine). Button is

<button class="flat_button popup_login_btn button_big" id="install_allow" type="submit" onclick="return login(this)">Button</button>

In Java:

  HTMLInputElement confirmButton =
    (HTMLInputElement) document.getElementsByTagName("button").item(0);

  confirmButton.click();

Console error:

java.lang.ClassCastException: com.sun.webpane.webkit.dom.HTMLButtonElementImpl cannot be cast to org.w3c.dom.html.HTMLInputElement

RESEARCH: I could try it Javascript way, like here: https://stackoverflow.com/a/20458003/1112963

QUETION: How to click a webpage button a native JavaFX WebEngine way?

Community
  • 1
  • 1
Zon
  • 18,610
  • 7
  • 91
  • 99

2 Answers2

0

You can do this as outlined in the documentation https://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html in the section "Calling back to Java from JavaScript".

Make this class in your webEngine class:

public class JavaButtonClicker {
    public void doSomething() {
        JOptionPane.showMessageDialog(null,
                                      "Click Sound");
    }}

Put this line in your html:

<button  onclick="clicker.doSomething()">Click here to make sound</button>

Make a method to do the click in java:

private void doClickInJava() {
JSObject window = (JSObject) webEngine. executeScript("window");              
                window.setMember("clicker",
                                 new JavaButtonClicker());
                webEngine.executeScript("clicker.doSomething()");
            }

Now when the user clicks the button on the page or when you invoke doClickInJava from your webEngine code, it calls doSomething method in JavaButtonClicker.

0
    HTMLInputElementImpl confirmButton =
    (HTMLInputElementImpl) document.getElementsByTagName("button").item(0);
    confirmButton.click();

use HTMLInputElementImpl instead of HTMLinputElement

EverNight
  • 964
  • 7
  • 16