小编Kun*_*ore的帖子

HtmlUnit无法登录它在表单提交后返回相同的页面

我试图在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();
Run Code Online (Sandbox Code Playgroud)

单击登录按钮后,同一页面将返回填充的输入字段.所以请帮帮我.谢谢

java htmlunit

6
推荐指数
1
解决办法
4495
查看次数

使用HtmlUnit进行抓取时出现OutOfMemoryError

我正在使用HtmlUnit登录到一个站点,然后从表中下载数据

当我运行我的代码是导致java.lang.OutOfMemoryError并且无法进一步运行.

以下是我的代码:

WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
                            webClient.getOptions().setPrintContentOnFailingStatusCode(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setTimeout(50000);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setPopupBlockerEnabled(true);

HtmlPage htmlPage=webClient.getPage(url);
Thread.sleep(200);
                            //~~~~~~~Log-In
HtmlTextInput uname=(HtmlTextInput)htmlPage.getFirstByXPath("//*[@id=\"username\"]");
uname.setValueAttribute("xxx");
HtmlPasswordInput upass=(HtmlPasswordInput)htmlPage.getFirstByXPath("//*[@id=\"password\"]");
upass.setValueAttribute("xxx");
HtmlSubmitInput submit=(HtmlSubmitInput)htmlPage.getFirstByXPath("//*[@id=\"login-button\"]/input");
htmlPage=(HtmlPage) submit.click();
Thread.sleep(200);
webClient.waitForBackgroundJavaScript(10000);
for (int i = 0; i < 250; i++) {
 if (!htmlPage.asText().contains("Loading...")) {
     break;
  }
    synchronized (htmlPage) {
     htmlPage.wait(500);
 }
}

System.out.println(htmlPage.asText());
Run Code Online (Sandbox Code Playgroud)

以下是stackTrace

java.lang.OutOfMemoryError: Java heap space
at net.sourceforge.htmlunit.corejs.javascript.Node.newString(Node.java:155)
at net.sourceforge.htmlunit.corejs.javascript.Node.newString(Node.java:151)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.createPropertyGet(IRFactory.java:1990)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.transformPropertyGet(IRFactory.java:968)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.transform(IRFactory.java:106)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.transformPropertyGet(IRFactory.java:964)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.transform(IRFactory.java:106)
at net.sourceforge.htmlunit.corejs.javascript.IRFactory.transformPropertyGet(IRFactory.java:964)
at …
Run Code Online (Sandbox Code Playgroud)

java htmlunit web-scraping tomcat7

6
推荐指数
1
解决办法
958
查看次数

标签 统计

htmlunit ×2

java ×2

tomcat7 ×1

web-scraping ×1