我正在尝试编写一个自动更新页面内容的应用程序(在我的帐户内)。我使用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>我该怎么解决它呢?谢谢。
发布于 2017-01-23 01:28:43
你的问题没有简单的解决方案,但你可以做一些事情。
不支持的javascript方法的提示
在按钮点击之后调用waitForBackgroundJavaScript;也许重定向是由一些javascript完成的,只有很小的延迟。
HtmlPage page2 = button.click();
client.waitForBackgroundJavaScript(30000);因为javascript可能已经更改了页面内容,所以您必须再次获取页面内容。
page2 = page2.getEnclosingWindow().getEnclosedPage();通常,对浏览器版本的检查是由一些javascript魔法完成的。也许你的网站使用的魔术并不(正确地)被HtmlUnit支持/模仿。如果你能找到这个问题的根本原因,你就可以补上一个bug (有关如何找到这个问题的一些提示,请参阅http://htmlunit.sourceforge.net/submittingJSBugs.html )。
https://stackoverflow.com/questions/41793267
复制相似问题