0

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.

Salim
  • 199
  • 3
  • 18
  • may i know what is xpath? – Gaali Prabhakar Oct 06 '15 at 18:09
  • XPath, the XML Path Language, is a query language for selecting nodes from an XML document. The xpath of google.com search box is //*[@id="lst-ib"] – Salim Oct 06 '15 at 18:48
  • So wait, you would like to load up google, you want to click the search box, get the xpath (for the search box?) and than put that xpath into the search box? Could you reword the question for me? – jagdpanzer Oct 06 '15 at 20:28
  • You cannot do it in Java, but you might be able to find a solution with JavaScript and inject that into your loaded page. But I'm not that solid in JavaScript myself. – hotzst Oct 06 '15 at 20:43
  • @brightbomb I need the xpath to return in system.out. – Salim Oct 06 '15 at 20:47
  • @hotzst I've tried that. I'm not very good with communicating java and javascript. Any ideas where I can start? – Salim Oct 06 '15 at 20:47
  • Have a look at this http://stackoverflow.com/questions/32751342/javafx-webengine-overwriting-a-websites-stylesheet-with-local-files/32816466#32816466 for injecting JavaScript into the WebView and this might help with the XPath: http://stackoverflow.com/questions/2661818/javascript-get-xpath-of-a-node – hotzst Oct 06 '15 at 20:50

1 Answers1

1

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 Strings from JS to Java, which works perfectly, I don't know if it works for differnt kinds of objects.

Community
  • 1
  • 1
Westranger
  • 1,308
  • 19
  • 29