gpt4 book ai didi

java - 使用 HtmlUnit 登录 Gmail

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:47 26 4
gpt4 key购买 nike

我正在尝试自动登录 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

最佳答案

内置浏览器可能无法正确解析Javascript。

尝试模拟不同的浏览器。来自 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();
}

此代码段模拟 Firefox v17。您可以查看受支持的可模拟浏览器的完整列表 in these javadocs 。今天,这些是:

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

关于java - 使用 HtmlUnit 登录 Gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26428699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com