java 使用 HtmlUnit 登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login Using HtmlUnit
提问by Renjith K N
I am very very new to HtmlUnit. I want to know do I am able to login to a site using htmlunit and perform some operations in the site for example I want to login to my office portal and to aplly a leave.
我对 HtmlUnit 非常陌生。我想知道我是否能够使用 htmlunit 登录到一个站点并在该站点中执行一些操作,例如我想登录到我的办公室门户并请假。
我正在使用 html 单元,它显示了一些错误,是否可以使用 html 单元或 are there any other tools I can use for this purpose是否有任何其他工具可以用于此目的......这是我的代码
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
final HtmlPage page1 = webClient.getPage("http://www.ccstechnologies.org/login.aspx/");
final HtmlForm form = page1.getFormByName("form1");
final HtmlSubmitInput button = form.getInputByName("BtnLogin");
final HtmlTextInput textField = form.getInputByName("Username");
final HtmlPasswordInput pwd = form.getInputByName("password");
textField.setValueAttribute("username");
pwd.setValueAttribute("password");
final HtmlPage page2 = button.getEnclosingForm().click();
String htmlBody = page2.getWebResponse().getContentAsString();
System.out.println("Base Uri 1 : "+page1);
System.out.println("Base Uri 2 : "+page2);
webClient.closeAllWindows();
But when I print page2
it shows only the login page's url, and it is not returning the homepage url. What could be the problem ?
但是当我打印page2
它时只显示登录页面的 url,它没有返回主页 url。可能是什么问题呢 ?
This is what I got in my console when clicked on form
这是单击表单时我在控制台中得到的
May 28, 2012 11:44:15 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered:'application/x-javascript'. May 28, 2012 11:44:16 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. Base Uri 1 : HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851 Base Uri 2 : HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851
2012 年 5 月 28 日上午 11:44:15 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl 通知警告:遇到过时的内容类型:'application/x-javascript'。2012 年 5 月 28 日上午 11:44:16 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl 通知警告:遇到过时的内容类型:'application/x-javascript'。基本 Uri 1:HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851 基本 Uri 2:HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851
results generated when clicked on button
单击按钮时生成的结果
May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: [259:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: [259:29] Error in style rule. Invalid token "\r\n ". Was expecting one of: "}", ";".
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning
WARNING: CSS warning: [259:29] Ignoring the following declarations in this rule.
HtmlPage(http://192.168.0.5/login.aspx)@23511316
HtmlPage(http://192.168.0.5/login.aspx)@17700115
回答by Joe Yahchouchi
Ok, I looked into it, it would seem the problem was with the button. I replaced you line of code for the button with this:
好吧,我看了看,似乎问题出在按钮上。我用这个替换了按钮的代码行:
final HtmlPage page2 = (HtmlPage) form.getInputByValue("Login").click();
now it appears that it at least tries to login(and the page of course prints invalid login) so it should work with appropriate credentials. to print the page in java and see it use system.out.println(page1.asText()) or asXml depending on what you want to see
现在看来它至少尝试登录(当然页面会打印无效登录),因此它应该使用适当的凭据。在java中打印页面并查看它使用system.out.println(page1.asText())或asXml,具体取决于您想要查看的内容
my code is finally this:
我的代码终于是这样的:
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
try{ final HtmlPage page1 = webClient.getPage("http://www.ccstechnologies.org/login.aspx/");
final HtmlForm form = page1.getFormByName("form1");
final HtmlSubmitInput button = form.getInputByName("BtnLogin");
final HtmlTextInput textField = form.getInputByName("Username");
final HtmlPasswordInput pwd = form.getInputByName("password");
textField.setValueAttribute("username");
pwd.setValueAttribute("password");
System.out.println(page1.asText());
final HtmlPage page2 = (HtmlPage) form.getInputByValue("Login").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
}
回答by Kunbi
this is what you should set for the javascript:
这是您应该为 javascript 设置的内容:
webClient.getOptions().setJavaScriptEnabled(false);
and you can as well add these to it.
您也可以将这些添加到其中。
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getCookieManager().setCookiesEnabled(true);
This should solve the problem as it did for me.
这应该像对我一样解决问题。
回答by Varun Tulsian
If the website uses an ajax call to login. This worked for me. Set this
如果网站使用ajax调用登录。这对我有用。设置这个
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
This would cause all ajax calls to be synchronous.
这将导致所有 ajax 调用都是同步的。
This is how I setup my WebClient object
这就是我设置 WebClient 对象的方式
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getCookieManager().setCookiesEnabled(true);
回答by Joe Yahchouchi
try to set cookies enabled, and try to set javascript enabled ignore errors that it might print...(I used to think errors in red are bad, in html unit it seems not necessarily)
尝试设置 cookie 启用,并尝试设置 javascript 启用忽略它可能打印的错误......(我曾经认为红色错误是坏的,在 html 单元中似乎不一定)