2

I'm not really sure how to ask this question but here is what I am trying to do. I am looking for the ability to load a website in java application and being able to click the buttons, text, box, etc and get the underlying DOM code.

For example:
-It loads google.com the webpage as well as the LIVE DOM under it, different frame. It can't be HTML since google is javascript.

-I want to click the google search box, or anything on the page, and application will print any attribute, such as name="q" or just q.

I've looked into XULrunner but it is deprecated for java.

Salim
  • 199
  • 3
  • 18

2 Answers2

0

You can use JavascriptExecutor for that. It is implemented by all WebDrivers. See JavaDoc for details. Use documentto refer to the DOM.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • Are you saying inject javascript into the page after it loads? Like, adding onclick="return value of name" to the search box? – Salim Sep 24 '15 at 12:36
  • Depends on what you want to do. But, yes, you can execute any Javascript on the page. – Würgspaß Sep 24 '15 at 12:37
0

You can use JavaFX WebView. You can call back to your Java code from Javascript. So you could write a small amount of Javascript that detects when the user clicked on element, and call some Java code that shows attribute value, for example. See "Calling back to Java from Javascript" in the WebEngine API docs.

Sample code:

public class HTMLMouseOverTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        final BorderPane root = new BorderPane();
        final WebView webView = new WebView();
        final WebEngine engine = webView.getEngine();
        final BooleanProperty mouseOver = new SimpleBooleanProperty();
        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {

            @Override
            public void changed(ObservableValue<? extends State> observable,
                    State oldValue, State newValue) {
                if (newValue == State.SUCCEEDED) {
                    // Here's how to add the Javascript if you don't have
                    // direct access to the HTML:
//                    final Document doc = engine.getDocument();
//                    Element div = doc.getElementById("important-div");
//                    div.setAttribute("onmouseover", "mouseOverProperty.set(true)");
//                    div.setAttribute("onmouseout", "mouseOverProperty.set(false)");
                    final JSObject window = (JSObject) engine.executeScript("window");
                    window.setMember("mouseOverProperty", mouseOver);                    
                }
            }
        });
        engine.loadContent("<html><body style='font-family:sans-serif';><h2>Hello World</h2>"+
            "<div id='important-div' onmouseover='mouseOverProperty.set(true)'"+
                "onmouseout='mouseOverProperty.set(false)' style='background: #ffd; padding:40px;'>"+ 
            "Move mouse here</div>"+
            "<h3>Thanks and good night</h3></body></html>");

        root.setCenter(webView);
        final Label label = new Label();
        label.textProperty().bind(Bindings.when(mouseOver).then("Mouse in position").otherwise("Mouse out of area"));
        root.setBottom(label);
        final Scene scene = new Scene(root, 400, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
nsa_a1
  • 101
  • 4
  • This looks promising. Do you know any sites that have examples close to what I am trying to do? – Salim Sep 24 '15 at 12:41
  • You can see the example on the Internet (https://labs.mwrinfosecurity.com/advisories/2013/09/24/webview-addjavascriptinterface-remote-code-execution/) – nsa_a1 Sep 24 '15 at 13:11