簡體   English   中英

從 JavaFX 中的 WebView 訪問元素

[英]Accessing elements from WebView in JavaFX

WebView訪問網頁元素的最簡單和最優雅的方法是什么? 在瀏覽器中檢查元素時,我們還可以通過名稱訪問我們知道的元素,例如stackoverflow頂部的搜索表單嗎?

我閱讀了一些關於自動填充文本字段的類似問題和答案,來自jewelseaJames_D和其他一些貢獻者,但至少說起來似乎很復雜。

我將在我創建一個WebView地方包含簡短的代碼,如果有人能以簡單易懂的方式完成這項任務,那就太好了。

郵件類

public class Mail extends Region{
    final WebView webView = new WebView();
    final WebEngine webEngine = webView.getEngine();

    public Mail() {
        webEngine.load("https://mail.metropolitan.ac.rs/?client=advanced#1");
        getChildren().add(webView);
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(mail, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
    }

mail添加到StackPane

public class Controller implements Initializable {
private Mail mail;

// Unrelated code

@FXML
    private void selectMail() {
                mail = new Mail();
                stackPane.getChildren().add(mail);
            }
    }

// Unrelated code

}

假設我們想要訪問兩個 TextField 來自動填充表單。

這是我在檢查用戶名文本字段后得到的:

<input name="username" class="zLoginField" id="username" type="text" size="40" maxlength="1024" value="" autocorrect="off" autocapitalize="off">

這是密碼文本字段:

<input name="password" class="zLoginField" id="password" type="password" size="40" maxlength="1024" value="" autocomplete="off">

注意:這不是一個任務或類似的東西,我只是在搞亂它,所以沒有任何成功。 歡迎所有示例,無論是我的還是你的。

您想使用 WebEngine 的Document 屬性

要訪問您的輸入字段:

Element inputField = webEngine.getDocument().getElementById("username");
inputField.setAttribute("value", "New Text");

對於 Stack Overflow 搜索表單之類的內容,您可以使用 XPath 來查找元素:

Element inputField = (Element)
    XPathFactory.newInstance().newXPath().evaluate(
        "//*[@id='search']//*[@name='q']",
        webEngine.getDocument(),
        XPathConstants.NODE);
inputField.setAttribute("value", "New Text");

根據評論更新:要在頁面仍在加載時設置字段,您可以嘗試在 WebEngine 更新其 loadWorker 時設置它:

public class HtmlInputSetter
extends Application {
    private static final String NEW_TEXT = "NullPointerException";

    private WebView webView;

    private boolean fieldsHaveBeenSet;

    @Override
    public void start(Stage stage) {
        webView = new WebView();
        webView.getEngine().getLoadWorker().progressProperty().addListener(
            (o, old, progress) -> updateFields());

        fieldsHaveBeenSet = false;
        webView.getEngine().load("http://stackoverflow.com");

        BorderPane pane = new BorderPane(webView);
        pane.setPrefWidth(1200);

        stage.setScene(new Scene(pane));
        stage.setTitle("HTML Input Setter");
        stage.show();
    }

    private void updateFields()
    {
        Document doc = webView.getEngine().getDocument();
        if (doc != null && !fieldsHaveBeenSet) {
            try {
                Element inputField = (Element)
                    XPathFactory.newInstance().newXPath().evaluate(
                        "//*[@id='search']//*[@name='q']",
                        doc, XPathConstants.NODE);
                if (inputField != null) {
                    inputField.setAttribute("value", NEW_TEXT);
                    fieldsHaveBeenSet = true;
                }
            } catch (XPathException e) {
                e.printStackTrace();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM