简体   繁体   中英

Java WebEngine.executeScript not working for forms or button clicks

I am trying to click a button using Java FX's class, WebEngine. I am able to interact with the page to do other things, like read from a text box using the following:

webEngine.executeScript("document.getElementsByName(\"myText\")[0].value = \"Test\";");

The above snippet works fine, but when I attempt to execute the below three examples to push a button/submit a form, nothing happens at all.

document.myForm.submit();

document.getElementById(\"myForm\").submit();

document.getElementById("theSubmitButton").click();

All three of the three above work when using the Firefox Web Console, but not when I change them to webEngine.executeScript();

Any idea on what would be causing this to not work in the Java webEngine.executeScript() call?

EDIT: Also, I know for a fact that the page is loaded, I have already set up something to wait for the page to load before calling anything.

EDIT2: Here is some more information about the Java call and results. When I call the below line:

System.out.println(webEngine.executeScript("document.getElementById('theSubmitButton').click();"));

I get the following results:

undefined

When I run this however:

System.out.println(webEngine.executeScript("document.getElementById('theSubmitButton');"));

I get this result:

[object HTMLInputElement]

So I know the object is present where I think it is, the issue is the .click() call. Again, the below code still works in Firefox's web console as shown below and does everything I need it to do.

document.getElementById('theSubmitButton').click();

When I use the .click() function in my java program (using .executeScript()), the button's text changes as I would see in any web browser, but the actual redirection and form submission that I need does not happen. When I use the graphical webEngine browser, it also does not work.

Form submission does work everywhere else (Firefox, Chrome, IE).

I have fixed the issue.

As it turns out, the text was not stored in .value (in the textarea) as I would have expected. When calling .value = "whatever", it locked up the browser. This was something Firefox handled gracefully, but webEngine did not.

Changing this to the .innerHTML that the text was actually stored in (not my design) fixed the issue, and calling .click() worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM