360SDN.COM

首页/Java/列表

HtmlUnit得到click之后网页的内容

来源:  2017-08-30 22:04:50    评论:0点击:

InputStream is = _link.click().getWebResponse().getContentAsStream();

 

InputStream is = _link.openLinkInNewWindow().getWebResponse().getContentAsStream();



1 - Login

2 - For each element (cars)

----- 3 Search for car

----- 4 Download zip file from a link
 

Creation of the webclient:

webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
provider.addCredentials(USERNAME, PASSWORD);
webClient.setCredentialsProvider(provider);
webClient.setRefreshHandler(new ImmediateRefreshHandler());

Login:

  public void login() throws IOException
  {
    page = (HtmlPage) webClient.getPage(URL);
    HtmlForm form = page.getFormByName("formLogin");

    String user = USERNAME;
    String password = PASSWORD;

    // Enter login and password
    form.getInputByName("LoginSteps$UserName").setValueAttribute(user);
    form.getInputByName("LoginSteps$Password").setValueAttribute(password);

    // Click Login Button
    page = (HtmlPage) form.getInputByName("LoginSteps$LoginButton").click();

    webClient.waitForBackgroundJavaScript(3000);

    // Click on Campa area
    HtmlAnchor link = (HtmlAnchor) page.getElementById("ctl00_linkCampaNoiH");
    page = (HtmlPage) link.click();

    webClient.waitForBackgroundJavaScript(3000);
    System.out.println(page.asText());
  }

Search for car in website:

private void searchCar(String _regNumber) throws IOException
 {
// Open search window
page = page.getElementById("search_gridCampaNoi").click();

webClient.waitForBackgroundJavaScript(3000);

// Write plate number
HtmlInput element = (HtmlInput) page.getElementById("jqg1");
element.setValueAttribute(_regNumber);

webClient.waitForBackgroundJavaScript(3000);

// Click on search
HtmlAnchor anchor = (HtmlAnchor) page.getByXPath("//*[@id=\"fbox_gridCampaNoi_search\"]").get(0);
page = anchor.click();

webClient.waitForBackgroundJavaScript(3000);
System.out.println(page.asText());
}

Download pdf:

    try
    {
      InputStream is = _link.click().getWebResponse().getContentAsStream();
      File path = new File(new File(DOWNLOAD_PATH), _regNumber);
      if (!path.exists())
      {
        path.mkdir();
      }
      writeToFile(is, new File(path, _regNumber + "_pdfs.zip"));
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }



来源:https://stackoverflow.com/questions/8049947/htmlunit-cant-retrieve-page-after-downloading-a-file

 

为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权