【问题标题】:HTMLUnit Java too old browserHTMLUnit Java 浏览器太旧
【发布时间】:2017-01-23 01:28:43
【问题描述】:

我正在尝试编写一个自动更新页面内容的应用程序(在我的帐户中)。我使用了 HTMLUnit,因为它支持 javascript。 但是我遇到了“您的浏览器太旧”的问题。

我的代码:

public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH);
        try (final WebClient client = new WebClient(BrowserVersion.FIREFOX_45)) {
            client.getOptions().setUseInsecureSSL(true);
            client.setAjaxController(new NicelyResynchronizingAjaxController());
            client.getOptions().setThrowExceptionOnFailingStatusCode(false);
            client.getOptions().setThrowExceptionOnScriptError(false);
            client.waitForBackgroundJavaScript(30000);
            client.waitForBackgroundJavaScriptStartingBefore(30000);
            client.getOptions().setCssEnabled(false);
            client.getOptions().setJavaScriptEnabled(true);
            client.getOptions().setRedirectEnabled(true);

            HtmlPage page = client.getPage("https://passport.yandex.ru/passport?mode=auth&retpath=https://toloka.yandex.ru/?ncrnd=5645");

            HtmlForm form = page.getForms().get(0);

            HtmlInput inputLogin = form.getInputByName("login");

            inputLogin.setValueAttribute(userName);
            HtmlInput inputPassw = form.getInputByName("passwd");

            inputPassw.setValueAttribute(password);

            DomElement button = page.getElementsByTagName("button").get(0);
            HtmlPage page2 = button.click();

            System.out.println(page2.asXml());
        }
        catch (IOException e) {

        }
    }

登录成功,但我无法加载第二页。 (它应该重定向到内容页面)

答案:

<h1 style="padding-top: 20px;">Browser is too old</h1>
    <p>
        Unfortunately you are using an old browser.
        Please, upgrade to at least IE10 or use one of the modern browsers, e.g.
        <a href="http://browser.yandex.net/">Yandex.Browser</a>,
        <a href="https://www.google.com/chrome/browser/">Google Chrome</a> or
        <a href="https://www.mozilla.org/firefox/new/">Mozilla Firefox</a>
    </p>

我该如何解决?谢谢。

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    您的问题没有简单的解决方案,但您可以做一些事情。

    1. 使用 HtmlUnit 的最新快照版本 (http://htmlunit.sourceforge.net/gettingLatestCode.html)
    2. 尝试使用不同的模拟浏览器(例如 chrome)
    3. 清理您的客户端设置,仅设置所需的选项(在您的情况下可能是 setUseInsecureSSL(true);
    4. waitForBackgroundJavaScript 和 waitForBackgroundJavaScriptStartingBefore 没有选项;在客户端设置中这样做是没有用的
    5. 检查您的日志;也许有一些关于不支持的 javascript 方法的提示

    在按钮点击后调用waitForBackgroundJavaScript;也许重定向是由一些 javascript 完成的,延迟很小。

    HtmlPage page2 = button.click();
    client.waitForBackgroundJavaScript(30000);
    

    而且由于 javascript 可能已经更改了页面内容,您必须再次获取页面内容。

    page2 = page2.getEnclosingWindow().getEnclosedPage();
    

    通常,浏览器版本的检查是通过一些 javascript 魔术来完成的。也许 HtmlUnit 不(正确地)支持/模拟您的网站使用的魔术。如果您能够找出导致此问题的根本原因,您可以填写一个错误(请参阅http://htmlunit.sourceforge.net/submittingJSBugs.html 了解如何找到此问题的一些提示)。

    【讨论】:

    • 谢谢,但它没有帮助。 2017 年 1 月 22 日晚上 8:57:38 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl 通知警告:遇到过时的内容类型:'application/x-javascript'。 (我不知道这是什么意思,因为我根本不懂 JS)
    • 那么,有没有办法在java中做这个应用程序?
    • “谢谢,但没有帮助” - 什么没有帮助?关于日志输出:这是一个警告,这个可以忽略。如果您想自动化网页,您需要对 Http、Html 和 Javascript 等网络基础知识有很好的了解。
    • 顺便说一句。您可以做的另一件事是使用 type(...) 函数而不是 setValueAttribute。 Type() 也通过触发键盘事件来模拟真实用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 2013-05-29
    • 2011-07-20
    相关资源
    最近更新 更多