gpt4 book ai didi

java - 如何从 Webview Javafx 获取元素

转载 作者:行者123 更新时间:2023-12-02 01:07:11 25 4
gpt4 key购买 nike

我有一个应用程序,我通过链接向网站提供值,然后该应用程序向我显示一张 map 并在给定值之间绘制一条折线。所以我现在需要的是我必须从 Webview 中提取特定值,所以在这篇文章 How to get the value of a Java variable to javascript in JavaFX webView? 中他们使用 getDocument 但我不理解代码,这就是为什么我不能很好地使用它。有人可以帮我吗?

显示 map 的代码:

@FXML
void openHtmlfile(ActionEvent event) {
WebEngine webEngine = webView.getEngine();
webEngine.javaScriptEnabledProperty().set(true);
webEngine.load("https://www.luftlinie.org/"+ fromTextField.getText() +"/"+ toTextField.getText());
;
}

从文本域中给出值并填充它们的方法

void fillFields(ActionEvent event) {

String startAirport = null;
String targetAirport = null;
String startCountry = null;
String targetCountry = null;

Statement airport;
try {
airport = connection.createStatement();
ResultSet MyRsAirport = airport.executeQuery("select * from fluglinie where ID =\""
+ idFlightRoute.getValue() + "\" AND fluggesellschaft =\"" + fluggesellschaft + "\"");
while (MyRsAirport.next()) {

startAirport = MyRsAirport.getString("startFlughafen");

targetAirport = MyRsAirport.getString("zielFlughafen");

}
Statement country;
country = connection.createStatement();
ResultSet MyRsCountry = country.executeQuery("select * from flughaefen");
while (MyRsCountry.next()) {

if (MyRsCountry.getString("name").equals(startAirport)) {
startCountry = MyRsCountry.getString("country");
} else if (MyRsCountry.getString("name").equals(targetAirport)) {
targetCountry = MyRsCountry.getString("country");
}

}

fromTextField.setText(startAirport + ", " + startCountry);
toTextField.setText(targetAirport + ", " + targetCountry);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我现在想做的是,例如,计算出这两点之间的距离,我想提取它。我知道我必须使用特定 html 元素的 id,但是如何在 java 中显示它?

最佳答案

永远记住,官方文档是你的 friend 。

首先,您需要等待页面完全加载。您可以通过观察 WebEngine 的 load worker 来做到这一点,其状态为 SUCCEEDED当它完成时。此方法在the WebEngine documentation中有详细描述。 .

webEngine.getLoadWorker().stateProperty().addListener(
(o, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
// ...
}
});
webEngine.load("https://www.luftlinie.org/"+ fromTextField.getText() +"/"+ toTextField.getText());

重要提示:您必须在加载页面之前添加 loadWorker 监听器。

一旦负载工作器成功完成,您就可以安全地访问 WebEngine 的 document ,其中包含加载的整个页面。您可以使用方便命名的 getElementById 来搜索具有特定 ID 的元素。方法:

webEngine.getDocument().getElementById("some-page-specific-ID")

您应该传递什么字符串取决于页面。您可以检查页面的源代码并找到所需内容的元素的 id

如果元素没有 id 属性,则必须依赖文档的结构并使用 XPath找到感兴趣的 HTML 元素。有关 XPath 使用的教程会使这个答案太大,但您可以通过阅读 the specification 自行了解它。 。阅读它需要 XML 的应用知识。

找到元素后,从中提取信息将取决于它是什么类型的元素。如果只是文本,可以使用元素的getTextContent()方法。

因此,整体代码将如下所示:

webEngine.getLoadWorker().stateProperty().addListener(
(o, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
Document doc = webEngine.getDocument();
Element element = doc.getElementById("some-page-specific-ID");
String value = element.getTextContent();

// Display value ...
}
});
webEngine.load("https://www.luftlinie.org/"+ fromTextField.getText() +"/"+ toTextField.getText());

关于java - 如何从 Webview Javafx 获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59867591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com