Listen changes on page using JxBrowser

亡梦爱人 提交于 2019-12-24 11:01:48

问题


In Java project I use JxBrowser.

I have page

<!DOCTYPE html>
<html>
<body>

Start: <input type="text" id="startTimeField"> ms
<hr>
<button id="buttonToClick" onclick="myFunction()">Start</button>

<script>
    function myFunction() {
       document.getElementById('startTimeField').value = new Date().getMilliseconds();
    }
</script>

</body>
</html>

I load page correct with JxBrowser.

When button "Start" is clicked, then value of the text input is setted.

I want add listener using JxBrowser object in Java to listen startTimeField text input changes.

Ex: startTimeField is setted by 0, after click Start button the field change value (by actual ms time) and I want detect this changes in Java code and get the value of change.


回答1:


Since the question basically asks how to listen to events in the DOM in Java, I think this is what you are looking for:

JxBrowser - DOM Events

You will have to do something similar to this (in your Java code):

DOMElement element = browser.getDocument().findElement(By.id("startTimeField"));
element.addEventListener(DOMEventType.OnChange, new DOMEventListener() {
    public void handleEvent(DOMEvent event) {
        // your code for reacting to the change here
    }
}, false);

I did not test this code but adapted it from the documentation, so it might not compile and work right away, but this should lead you on the right track.




回答2:


<input type="text" id="startTimeField" onchange="yourfunction">

Look at the reference



来源:https://stackoverflow.com/questions/42391142/listen-changes-on-page-using-jxbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!