Is there a way to get the xpath of different input/href/div of a page loaded in a javafx webview?
For example: I want to be able to load google.com Click search box return xpath of the search box in system.out.
Is there a way to get the xpath of different input/href/div of a page loaded in a javafx webview?
For example: I want to be able to load google.com Click search box return xpath of the search box in system.out.
Well I dont have an working example, but I can give you all the neccessary hinds you need. I also used this several times to communicate between Java and Javascript. What happens next is that you specify an Java class which will be injected into the Javascript part and which acts like a bridge between the two languages. First you need a callback class, which is called whenever you want to pass something from the JavaScript side to Java
import netscape.javascript.JSObject;
JSObject window = (JSObject) webView.getEngine().executeScript("window");
window.setMember("jsCallBack", new JSCallBack());
The callback class need at least one method which can be called from the Javascript side. in this case it is the callback()
method
public final class JSCallBack {
public JSCallBack() {}
public void callback(final String response) {
System.out.println(response) ; // this is the String which you passed on the JS side
}
}
Now it is possible to invoke the callback()
method from the Javascript side and it is also possible to pass arguments.
On the Javascript side you can call the callback function of the previously injected object by
function myCallback(value){
jsCallBack.callback(value);
}
The next thing you need to do is to specify a listener in Javascript, which listens for mouse events. There is already an existing post which copes with the problem of assembling the xpath for a clicked elements. After the assembly you only need to pass the result to the callback. On this blog you can also find an exmaple for communicating between JavaFx and Javascrit via callbacks.
So fa I only have experience in passing String
s from JS to Java, which works perfectly, I don't know if it works for differnt kinds of objects.