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.