简体   繁体   English

HtmlUnit在单击一个按钮时引发许多异常

[英]HtmlUnit throws many exceptions on clicking one button

I'm trying to create program that signs in to Yahoo account. 我正在尝试创建登录Yahoo帐户的程序。 I'm using HtmlUnit in Java, but when the program is trying to cick Sign In button it throws a large number of various exceptions. 我在Java中使用HtmlUnit ,但是当程序尝试单击“ 登录”按钮时,它将引发大量的各种异常。

The button form is: 按钮形式为:

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

and my java code: 和我的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();
    }
}

All exceptions is in the line HtmlPage nextPage = (HtmlPage) signInButton.click(); 所有异常都在HtmlPage nextPage = (HtmlPage) signInButton.click();

After this line program is logged in Yahoo account but these all exceptions somehow affects all program work that it works unstably. 在此行程序登录到Yahoo帐户后,但是所有这些异常都会以某种方式影响其不稳定运行的所有程序工作。 What should I do to avoid this situation or at least catch all exceptions? 我应该怎么做才能避免这种情况或至少捕获所有异常?

From the errors you pasted it looks like one of those cases where the HTMLUnit Javascript implementation differs from the one in browser. 从您粘贴的错误中可以看出,HTMLUnit Javascript实现不同于浏览器中的一种。 I also wouldn't be surprised if Yahoo didn't want people to perform automatic login into their properties. 如果Yahoo不希望人们对他们的属性执行自动登录,我也不会感到惊讶。

Instead of wrestling with Javascript here I would try the following things: 除了尝试在此处使用Javascript之外,我还可以尝试以下操作:

  1. Look at HTTP communication using a sniffer (like HttpFox) and try to send login request using HTTPURLConnection or HTTPClient. 查看使用嗅探器(例如HttpFox)的HTTP通信,并尝试使用HTTPURLConnection或HTTPClient发送登录请求。
  2. See if you can login using the API. 查看是否可以使用API​​登录。 I know Yahoo provides OpenID, they also might provide OAuth authentication, see http://developer.yahoo.com/oauth/ 我知道Yahoo提供了OpenID,他们也可能提供了OAuth身份验证,请参阅http://developer.yahoo.com/oauth/
  3. Use Selenium to login using real browser. 使用Selenium通过真实的浏览器登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM