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

HTMLUNIT仅在调试时可重定向至最终页面,正常运行时失效问题求助

HTMLUNIT仅在调试时可重定向至最终页面,正常运行时失效问题求助

各位大佬求助!我在用HTMLUNIT 2.67.0版本实现登录功能时遇到了一个诡异的问题:逐行调试代码时,我能正常输入邮箱、密码并跳转到登录后的最终页面,但直接运行代码的话,就一直卡在「Wait... Redirecting to the Final Page」这个跳转页面。我试了各种办法——比如用新线程执行关键代码、加同步锁等等,都没用,实在没辙了,求支招!😭

下面是我的代码,麻烦帮忙看看哪里出问题了:

核心业务代码

WebClient webClient = createWebClient();

try {
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

    HtmlPage page = webClient.getPage("https://pops.ons.org.br/ons.pop.federation");
    threadWait(2);

    HtmlSubmitInput button = (HtmlSubmitInput) page.getElementByName("submit.IdentificarUsuario");
    threadWait(1);

    HtmlTextInput textField1 = (HtmlTextInput) page.getElementById("username");
    threadWait(1);
    textField1.setValueAttribute("email@login.com");
    threadWait(2);

    button.click();
    threadWait(3);

    // 重新获取页面以便输入密码
    page = webClient.getPage("https://pops.ons.org.br/ons.pop.federation");
    threadWait(3);

    HtmlPasswordInput textField2 = (HtmlPasswordInput) page.getElementById("password");
    threadWait(1);

    HtmlSubmitInput button2 = (HtmlSubmitInput) page.getElementByName("submit.Signin");
    threadWait(1);
    textField2.setValueAttribute("myPwd123");
    threadWait(2);

    // 关键部分:点击登录按钮
    synchronized(button2) {
        button2.click();
    }
    threadWait(5);

    // 另一个关键部分:重新获取页面
    // 调试时这步能拿到最终页面,但正常运行就卡在跳转页
    synchronized (page) {
        page = webClient.getPage("https://pops.ons.org.br/ons.pop.federation");
    }
    threadWait(10);

    System.out.println(page.asXml());
}

createWebClient()方法

public WebClient createWebClient() {
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    try {
        // WebClient配置参数
        webClient.setJavaScriptTimeout(10000);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(0);
        webClient.getOptions().setRedirectEnabled(true);
        
        java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
        java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
        
        return webClient;
    } catch (Exception e) {
        logger.error("Error when creating WebClient: {}", e.getMessage());
        return null;
    }
}

threadWait()方法(模拟真实用户操作间隔)

public void threadWait(int segundos) {
    try {
        Random r = new Random();
        int min = 1000;
        int max = 5001;
        int random = r.nextInt(max - min) + min;
        segundos = (segundos * 1000) + random;
        Thread.sleep(segundos);
    } catch (Exception e) {
        System.out.println("Error in Thread.sleep() ... " + e.getMessage());
    }
}

我查了不少相关帖子,大家都说用Thread.sleep()模拟调试时的等待时间就能解决,但我这么做了还是不管用。到底怎么才能让代码正常运行时也能成功跳转到最终页面啊?

备注:内容来源于stack exchange,提问作者gbossa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 13:17:30