Limmy
Limmy

Reputation: 746

HTMLUnit Java too old browser

I'm trying to write an application for automatic update page content (inside my account). I used the HTMLUnit because it supports javascript. But I faced with "your browser is too old" problem.

My code:

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) {

        }
    }

Login is successful, but I can't load second page. (It should redirect to content page)

Answer:

<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>

How can I solve it? Thanks.

Upvotes: 0

Views: 640

Answers (1)

RBRi
RBRi

Reputation: 2879

There is no simple solution for your problem but there are some things you can do.

  1. use the latest snapshot build of HtmlUnit (http://htmlunit.sourceforge.net/gettingLatestCode.html)
  2. try with different simulated browsers (e.g. chrome)
  3. cleanup your client settings, set only the options required (in your case maybe setUseInsecureSSL(true);
  4. waitForBackgroundJavaScript and waitForBackgroundJavaScriptStartingBefore are no options; doing this at client setup is useless
  5. check your log; maybe there are some hints about not supported javascript methods

Place the call of waitForBackgroundJavaScript after the button click; mabey the redirect is done by some javascript with a small delay.

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

And because the javascript might have change the page content you have to get the page content again.

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

Usually the checks for the browser version are done by some javascript magic. Maybe the magic trick used by your web site is not (correctly) supported/emulated by HtmlUnit. If you are able to find out the root cause for this you can fill a bug (see http://htmlunit.sourceforge.net/submittingJSBugs.html for some hints how to find this).

Upvotes: 1

Related Questions