0

I'm working on a JavaFX project. For some complicated reason, I need to get all visible text in a WebView. In addition, visible text changes over time, so I need to get it whenever it changes as well. I'm loading a php file in the engine.

I tried using view.getAccessibleText() and engine.getDocument().getTextContent() with no return values whatsoever. I also tried looping until I get some return values from the previous methods - again, no success. The answer in the link below doesn't really help because it is providing all underlying html code (and javascript).

get the contents from the webview using javafx

    WebView view = new WebView();
    WebEngine engine = view.getEngine();
    engine.setJavaScriptEnabled(true);
    engine.load("php-file");
    StackPane root = new StackPane();
    root.getChildren().add(view);
    Scene scene = new Scene(root, 500, 500);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.show();

    Task<Boolean> task = new Task<Boolean>() {

    @Override
    protected Boolean call() throws Exception {
        int counter = 0;
        while(view.getAccessibleText() == null || view.getAccessibleText().isEmpty) { // or engine.getDocument().getTextContent()
            if (counter == 300) {
                return false;
            }
            Delay.delay(1000); // Delays for 1000 ms
            counter++;  
        }
        System.out.println(view.getAccessibleText());
        if (view.getAccessibleText().contains("desiredText")) {
            return true;
        }
        return false;
    };

    task.setOnSucceeded(e-> {
        System.out.println(task.getValue());
    };
    Thread t = new Thread(task);
    t.start();

PHP file

http://www.mediafire.com/file/rrfocuwjq2ycs17/2.php/file

Please note that I have moderate experience in php, and very little experience in javascript. This php was provided by a co-worker. So a way through java is appreciated.

hman_codes
  • 794
  • 9
  • 24
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – SedJ601 Dec 12 '18 at 20:47
  • @Sedrick is it really a debugging help question? As in the title and the first paragraph, I explained the desired result. In the second one I mentioned the methods I used, and the answer in the linked question. The remainder of the code is simple stage and adding nodes to it. I will add code once im on my pc again, but i'm genuinely asking if my question is actually flawed – hman_codes Dec 12 '18 at 21:12
  • This is a "why isn't this code working" question. You need to follow the link in hopes that you can come back and edit this to be a better question. – SedJ601 Dec 12 '18 at 22:00
  • @Sedrick thank you for your help. I edited my question accordingly. – hman_codes Dec 13 '18 at 04:31
  • Could you also post your `PHP`? – SedJ601 Dec 13 '18 at 05:07
  • @Sedrick Surely. I provided a link to the php file in the OP. I don't know if that's allowed (I searched and there was nothing against it), but it's too long to post as code in the OP I think. – hman_codes Dec 13 '18 at 07:43
  • You can probably try [this](https://www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery) plugin. – SedJ601 Dec 13 '18 at 07:53

0 Answers0