Login to gmail using HtmlUnit

∥☆過路亽.° 提交于 2019-12-24 11:07:05

问题


I am trying automate login to gmail but its not working. I am using HtmlUnit for form,textbox and submit button access. On passing correct username and password it displays all the services but in the end when i open gmail it shows the login page.

This is my code

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
    }

This is Error Log i am getting

    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

回答1:


It's possible that the built-in browser can't parse the Javascript correctly.

Try emulating a different browser. From the docs:

Often you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.

@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();
}

This snippet emulates Firefox v17. You can see the full list of supported, emulatable browsers in these javadocs. Today, these are:

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


来源:https://stackoverflow.com/questions/26428699/login-to-gmail-using-htmlunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!