You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用HtmlUnit与Java点击指定网页按钮?

无法用HtmlUnit 2.13点击目标按钮的问题求助

我是编程实习生,已经尝试了StackOverflow上的多种方案,仍然无法使用HtmlUnit(版本2.13)点击目标网站上ID为genr的按钮。目标网站地址:https://calculator888.ru/random-generator/sluchaynoye-slovo

目标按钮的HTML代码:

<div class="knop_pusk_blok">
   <input type="button" id="genr" data-tip="skivo" value="Случайное Слово" class="knop_dstv_vchl">
</div>

第一次尝试的代码:

public class Main2 {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("start");
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getCookieManager().setCookiesEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.waitForBackgroundJavaScriptStartingBefore(10000);
        webClient.waitForBackgroundJavaScript(10000);


        HtmlPage page = webClient.getPage("https://calculator888.ru/random-generator/sluchaynoye-slovo");


        List<HtmlElement> countOfWords = page.getByXPath("//*[@id=\"klch\"]");
        System.out.println("Count of words is " + countOfWords.size());

        List<HtmlElement> setOfWords = page.getByXPath("//*[@id=\"psz\"]");//word!!1111
        System.out.println("set of words is " + setOfWords.size());

        for (HtmlElement item : countOfWords)
            item.setAttribute("data-vpmn", "10");

        countOfWords = page.getByXPath("//*[@id=\"klch\"]");
        System.out.println("Count of words AFTER is " + countOfWords.get(0).getAttribute("data-vpmn"));

        //press button
        HtmlInput input = page.getHtmlElementById("genr");
        //Html + ButtonInput + Input + SubmitInput

        HtmlPage after = input.click();
        webClient.waitForBackgroundJavaScript(10000);

        for (int i = 0; i < 20; i++) {
            if (after.getByXPath("//*[@id=\"psz\"]").size() != 1) {
                break;
            }
            synchronized (page) {
                page.wait(500);
            }
        }
        

        List<HtmlElement> setOfWords1 = after.getByXPath("//*[@id=\"psz\"]");//after
        System.out.println("set of words is AFTER CLICK " + setOfWords1.size());

        System.out.println("end");
    }

}

修改后尝试的代码:

package org.example;


import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlElement;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {

        WebClient webClient = new WebClient();
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setJavaScriptEnabled(false);
        HtmlPage page = webClient.getPage("https://calculator888.ru/random-generator/sluchaynoye-slovo");
        webClient.waitForBackgroundJavaScript(10000);

        HtmlElement htmlInputLineOfWords = (HtmlElement) page.getByXPath("//*[@id=\"bov\"]").get(0);
        System.out.println("before click =" + htmlInputLineOfWords.getTextContent());
        webClient.waitForBackgroundJavaScript(10000);
        HtmlElement button = (HtmlElement) page.getElementById("genr");

        page = button.click();
        htmlInputLineOfWords = (HtmlElement) page.getByXPath("//*[@id=\"bov\"]").get(0);
        System.out.println("after click = " + htmlInputLineOfWords.getTextContent());
            System.out.println(page.asText());

    }
}

pom.xml依赖:

<dependency>
   <groupId>net.sourceforge.htmlunit</groupId>
   <artifactId>htmlunit</artifactId>
   <version>2.13</version>
</dependency>

已经耗时两天寻求解决方案,恳请帮助。

内容的提问来源于stack exchange,提问作者Lenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:09:53