0

我正在尝试单击一个没有 id,只有类的按钮。正因为如此,我不得不通过 xpath 检索元素,它有一个 Element 对象,它没有 click 方法。有谁知道一些策略来实现点击这个元素?

这是我获取元素的代码:

WebView wv = new WebView();
WebEngine we = wv.getEngine();
we.load("https://www.milanuncios.com/");

we.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
    if (newState == Worker.State.SUCCEEDED) {
        Document doc = we.getDocument();
        Element inputField = null;
        try {
            inputField =
                    (Element) XPathFactory.newInstance().newXPath().evaluate("//*[@class=\"ma-NavigationTopNav-mainActions-desktop\"]",
                            doc, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        if (inputField != null) {
            LOGGER.log(Level.INFO, "DENTRO "+inputField);
        }
    }
});
processPaneField.getChildren().add(wv);
4

1 回答 1

1

最后用js脚本解决:

we.executeScript("document.getElementsByClassName('ma-NavigationTopNav-mainActions-desktop')[0].click()");

于 2021-07-21T00:52:43.983 回答