我正在尝试在 HtmlUnit 的帮助下登录该站点,但是单击登录后,它返回相同的页面,其中输入字段填充了我无法登录的值,请建议我一个解决方案。
我正在尝试以下代码
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
String url="http://xxxxxxxxx.xxx/";
String name="XXXX";//here real value i am putting for name, accountNo and pass instead of XXXX
String accountNo="XXXX";
String pass="XXXX";
HtmlPage page = webClient.getPage(url);
System.out.println("1st page : "+page.asText());
HtmlForm form=(HtmlForm)page.getElementById("aspnetForm");
HtmlInput uName=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textUserName_text\"]").get(0);
uName.setValueAttribute(name);
HtmlInput acNo=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textCompanyAccount_text\"]").get(0);
acNo.setValueAttribute(accountNo);
HtmlPasswordInput password=(HtmlPasswordInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textPassword\"]").get(0);
password.setValueAttribute(pass);
HtmlSubmitInput button = (HtmlSubmitInput) form.getByXPath("//*[@id=\"ctl00_LoginControl_buttonLogin\"]").get(0);
page = (HtmlPage) button.click();
System.out.println("2nd Page : "+page.asText());
webClient.closeAllWindows();
单击登录按钮后,相同的页面将返回填充的输入字段。所以请帮帮我。谢谢