Java 使用HtmlUnit登录gmail

Java 使用HtmlUnit登录gmail,java,servlets,htmlunit,Java,Servlets,Htmlunit,我正在尝试自动登录到gmail,但它不工作。我使用HtmlUnit访问表单、文本框和提交按钮。在传递正确的用户名和密码时,它会显示所有服务,但最后当我打开gmail时,它会显示登录页面 这是我的密码 final WebClient webClient = new WebClient(BrowserVersion.CHROME); // Get the first page webClient.getOptions().setJavaScriptEnabled(true);

我正在尝试自动登录到gmail,但它不工作。我使用HtmlUnit访问表单、文本框和提交按钮。在传递正确的用户名和密码时,它会显示所有服务,但最后当我打开gmail时,它会显示登录页面

这是我的密码

final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    // Get the first page
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getCookieManager().setCookiesEnabled(true);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);

    try {
        final HtmlPage page1 = webClient.getPage("http://www.gmail.com/");
        final HtmlForm form = page1.getForms().get(0);
        final HtmlSubmitInput button = form.getInputByName("signIn");
        final HtmlTextInput textField = form.getInputByName("Email");
        final HtmlPasswordInput pwd = form.getInputByName("Passwd");
        textField.setValueAttribute("username");
        pwd.setValueAttribute("password");
        System.out.println(page1.asText());
        final HtmlPage page2 = (HtmlPage) button.click();

        String htmlBody = page2.getWebResponse().getContentAsString();
        System.out.println(page2.asText());
        System.out.println("Base Uri 1 : " + page1);
        System.out.println("Base Uri 2 : " + page2);

        webClient.closeAllWindows();
    } catch (Exception e) {
        // TODO: handle exception
    }
这是我收到的错误日志

    Oct 17, 2014 8:51:09 PM com.gargoylesoftware.htmlunit.html.InputElementFactory createElementNS
INFO: Bad input type: "email", creating a text input
Gmail
One account. All of Google.
Sign in to continue to Gmail
Email username Password password Sign in checked Stay signed in
For your convenience, keep this checked. On shared devices, additional precautions are recommended. Learn more
Need help?
Create an account
One Google Account for everything Google
About Google
Privacy & Terms
Help
?English (United States)?
Oct 17, 2014 8:51:20 PM com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runSingleJob
SEVERE: Job run failed with unexpected RuntimeException: CustomError: Error in protected function: Cannot call method "Xi" of null (script in https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3B5Ez3KUPqv3733OfFD97D_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=AItRSTMh1uD80u0Q-oPCPkAWIHZAIOe3nQ from (57, 18) to (114, 10)#67)
======= EXCEPTION START ========
Exception class=[net.sourceforge.htmlunit.corejs.javascript.JavaScriptException]
com.gargoylesoftware.htmlunit.ScriptException: CustomError: Error in protected function: Cannot call method "Xi" of null (script in https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3B5Ez3KUPqv3733OfFD97D_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=AItRSTMh1uD80u0Q-oPCPkAWIHZAIOe3nQ from (57, 18) to (114, 10)#67)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:705)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:637)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:612)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:1001)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptFunctionJob.runJavaScript(JavaScriptFunctionJob.java:53)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutionJob.run(JavaScriptExecutionJob.java:102)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)
    at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)
    at java.lang.Thread.run(Unknown Source)
Caused by: net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: [object Object] (script in https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3B5Ez3KUPqv3733OfFD97D_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=AItRSTMh1uD80u0Q-oPCPkAWIHZAIOe3nQ from (57, 18) to (114, 10)#67)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1006)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:630)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:690)
    ... 10 more
JavaScriptException value = [object Object]
== CALLING JAVASCRIPT ==

  function () {
      if (b.isDisposed()) {
          return a.apply(this, arguments);
      }
      try {
          return a.apply(this, arguments);
      }
      catch (c) {
          b.xa(c);
          if (!b.Ba) {
              throw b.Aa && ("object" === typeof c ? c.message = "Error in protected function: " + c.message : c = "Error in protected function: " + c), c;
          }
          throw new Yba(c);
      }
      finally {
      }
  }

======= EXCEPTION END ========

Oct 17, 2014 8:51:21 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:21 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:22 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:22 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:22 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:22 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 17, 2014 8:51:22 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Gmail
Basic HTML view
<style> #loading {display:none} </style><form action="?ui=html&amp;zy=c" method="post"><input type="hidden" name="at" value="AF6bupN_lVxTXggheNEbct_StIuAzk-YxQ"><font face=arial>JavaScript must be enabled in order for you to use Gmail in standard view. However, it seems JavaScript is either disabled or not supported by your browser. To use standard view, enable JavaScript by changing your browser options, then <a href="">try again</a>.<p>To use Gmail's basic HTML view, which does not require JavaScript, <input type="submit" value="click here" class="submit_as_link">.</p></font></form><form action="?ui=mobile&amp;zyp=c" method="post"><input type="hidden" name="at" value="AF6bupN_lVxTXggheNEbct_StIuAzk-YxQ"><p><font face=arial>If you want to view Gmail on a mobile phone or similar device <input type="submit" value="click here" class="submit_as_link">.</font></p></form>
Loading harishmohanani28@gmail.com…
Loading standard view | Load basic HTML (for slow connections)
This is taking longer than usual. Try reloading the page.
If that doesn't work, you can:
1. Disable Labs and try again.
2. If you're on a slow connection, try basic HTML view.
3. For more troubleshooting tips, visit the help center.

Skip to content
Using Gmail with screen readers
Keyboard shortcuts
Gmail (standard view) guide
This version of Chrome is no longer supported. Please upgrade to asupported browser. Dismiss
+Harish
+Harish
Search
YouTube
Maps
Play
News
Gmail
Drive
Calendar
Gmail
Add a shortcut
More
Translate
Books
Blogger
Photos
My Business
Even more from Google
Share
Change photo
Harish Mohanani
harishmohanani28@gmail.com
Account–Privacy
View profile
Harish Mohanani
harishmohanani28@gmail.com (default)
All your Google+ pages ›
Add account
Sign out
Search
Hidden fields
Gmail Logo





>
<
Conversations





Primary

Social

Promotions

>
<
English
English Dvorak
??????
???????? (???????)
?????? (INSCRIPT)
Enable personal dictionary
Hide Keyboard
Input Tools Settings
Base Uri 1 : HtmlPage(https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1)@26946257
Base Uri 2 : HtmlPage(https://mail.google.com/mail/u/0/)@16175221
2014年10月17日晚上8:51:09 com.gargoylesoftware.htmlunit.html.InputElementFactory CreateElements
信息:输入类型错误:“电子邮件”,正在创建文本输入
Gmail
一个帐户。所有的谷歌。
登录以继续使用Gmail
电子邮件用户名密码密码登录已签入保持登录
为方便起见,请检查此项。在共享设备上,建议采取其他预防措施。了解更多
需要帮助吗?
创建帐户
一个谷歌账户代表谷歌的一切
关于谷歌
隐私与条款
帮助
英语(美国)?
2014年10月17日晚上8:51:20 com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runSingleJob
严重:作业运行失败,出现意外的运行时异常:CustomError:受保护函数中的错误:无法调用null的方法“Xi”(中的脚本)https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3b5ez3kupqv3733 offd97d_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=aitrstmtmh1ud80u0q-oPCPkAWIHZAIOe3nQ from(57,18)至(114,10)#67)
======异常开始========
异常类=[net.sourceforge.htmlunit.corejs.javascript.JavaScriptException]
com.gargoylesoftware.htmlunit.ScriptException:CustomError:受保护函数中的错误:无法调用null的方法“Xi”(在https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3b5ez3kupqv3733 offd97d_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=aitrstmtmh1ud80u0q-oPCPkAWIHZAIOe3nQ from(57,18)至(114,10)#67)
在com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:705)
位于net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
位于net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
位于com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:637)
位于com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:612)
在com.gargoylesoftware.htmlunit.html.HtmlPage.executejavascriptfunctionifmable(HtmlPage.java:1001)上
位于com.gargoylesoftware.htmlunit.javascript.background.JavaScriptFunctionJob.runJavaScript(JavaScriptFunctionJob.java:53)
位于com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutionJob.run(JavaScriptExecutionJob.java:102)
位于com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)
在com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)
位于java.lang.Thread.run(未知源)
原因:net.sourceforge.htmlunit.corejs.javascript.JavaScriptException:[object object](中的脚本)https://mail.google.com/_/scs/mail-static/_/js/k=gmail.main.en.abFelT_TrjE.O/m=m_i,t,it/am=HCOG-3_3B5Ez3KUPqv3733OfFD97D_7vTQAk7ASA_83-H0Dvgf1Qfmg/rt=h/d=1/t=zcms/rs=AItRSTMh1uD80u0Q-oPCPkAWIHZAIOe3nQ从(57,18)到(114,10)#67)
位于net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretationLoop(Interpreter.java:1006)
位于net.sourceforge.htmlunit.corejs.javascript.Interpreter.interprefer(Interpreter.java:798)
位于net.sourceforge.htmlunit.corejs.javascript.transportedfunction.call(transportedfunction.java:105)
位于net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
在com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
位于net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057)
在net.sourceforge.htmlunit.corejs.javascript.transportedfunction.call(transportedfunction.java:103)
位于com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:630)
在com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:690)
…还有10个
JavaScriptException值=[object]
==调用JAVASCRIPT==
函数(){
if(b.isDisposed()){
返回a.apply(这个,参数);
}
试一试{
返回a.apply(这个,参数);
}
渔获物(c){
b、 xa(c);
如果(!b.Ba){
抛出b.Aa&(“对象”==typeof c?c.message=“受保护函数中的错误:”+c.message:c=“受保护函数中的错误:”+c),c;
}
抛出新的Yba(c);
}
最后{
}
}
=======异常结束========
2014年10月17日晚上8:51:21 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:21 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:22 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:22 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:22 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:22 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
2014年10月17日晚上8:51:22 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知
警告:遇到过时的内容类型:“text/javascript”。
Gmail
基本HTML视图
#为了在标准视图中使用Gmail,必须启用加载{display:none}JavaScript
@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}
static BrowserVersion   CHROME
static BrowserVersion   FIREFOX_17
static BrowserVersion   FIREFOX_24
static BrowserVersion   INTERNET_EXPLORER_11
static BrowserVersion   INTERNET_EXPLORER_8
static BrowserVersion   INTERNET_EXPLORER_9