我正在尝试创建登录Yahoo帐户的程序。 我在Java中使用HtmlUnit,但是当程序试图点击登录按钮时,会抛出大量各种异常。
按钮表格是:
<form method="post" action="https://login.yahoo.com/config/login?" autocomplete="" name="login_form" onsubmit="return hash2(this)">
<input type="hidden" name=".tries" value="1"/>
<input type="hidden" name=".src" value=""/>
<input type="hidden" name=".md5" value=""/>
<input type="hidden" name=".hash" value=""/>
<input type="hidden" name=".js" value=""/>
<input type="hidden" name=".last" value=""/>
<input type="hidden" name="promo" value=""/>
<input type="hidden" name=".intl" value="us"/>
<input type="hidden" name=".bypass" value=""/>
<input type="hidden" name=".partner" value=""/>
<input type="hidden" name=".u" value="eqn7kn96q7irv"/>
<input type="hidden" name=".v" value="0"/>
<input type="hidden" name=".challenge" value="rTRqt.vaVyBEJgxmMpkh0sqYx5Mz"/>
<input type="hidden" name=".yplus" value=""/>
<input type="hidden" name=".emailCode" value=""/>
<input type="hidden" name="pkg" value=""/>
<input type="hidden" name="stepid" value=""/>
<input type="hidden" name=".ev" value=""/>
<input type="hidden" name="hasMsgr" value="0"/>
<input type="hidden" name=".chkP" value="Y"/>
<input type="hidden" name=".done" value="http://my.yahoo.com"/>
<input type="hidden" name=".pd" value="_ver=0&c=&ivt=&sg="/>
<input type="hidden" name="pad" id="pad" value="3"/>
<input type="hidden" name="aad" id="aad" value="3"/>
<div id="inputs">
<div id="fun"/>
<div id="persistency">
<div id="submit">
<button type="submit" id=".save" name=".save" class="primaryCta" tabindex="5"> Sign In </button>
</div>
</form>
和我的java代码:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import java.io.IOException;
import java.net.URL;
public class VirtualWebBrowser {
public static void clickAuthorizeButton(String url, String login, String password) throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});
HtmlPage loginPage = (HtmlPage) webClient.getPage(url);
HtmlForm liginForm = loginPage.getFormByName("login_form");
liginForm.getInputByName("login").setValueAttribute(login);
liginForm.getInputByName("passwd").setValueAttribute(password);
HtmlButton signInButton = liginForm.getButtonByName(".save");
HtmlPage nextPage = (HtmlPage) signInButton.click();
webClient.closeAllWindows();
}
}
所有例外都在HtmlPage nextPage = (HtmlPage) signInButton.click();
此行程序在雅虎帐户中登录后,但这些所有异常都会以某种方式影响所有程序工作,它不能正常工作。 我该怎么做才能避免这种情况,或者至少要抓住所有例外情况?
答案 0 :(得分:1)
根据您粘贴的错误,它看起来就像HTMLUnit Javascript实现与浏览器中的实现不同的情况之一。如果雅虎不希望人们自动登录他们的房产,我也不会感到惊讶。
我不会在这里与Javascript搏斗,而是尝试以下方法: