htmlunit扒取JS加载之后的网页源码(1)

HTMLUnit其实就是一个用JAVA实现的无窗口的浏览器,可以很好的分析页面上的DOM元素,可以模拟浏览器的所有行为。多用于自动化测试。

HTMLUnit需要很多的jar包支持。所以推荐maven项目布置。

下面介绍简单的用法:

final WebClient webClient=new WebClient(BrowserVersion.CHROME);
    String url2 = "URL";
    System.out.println(" // 1 启动JS "); 
    webClient.getOptions().setJavaScriptEnabled(true);  
    System.out.println("// 2 禁用Css,可避免自动二次请求CSS进行渲染 "); 
    webClient.getOptions().setCssEnabled(false);  
    System.out.println("// 3 启动客户端重定向 "); 
    webClient.getOptions().setRedirectEnabled(true);  
    System.out.println("// 4 js运行错误时,是否抛出异常");  
    webClient.getOptions().setThrowExceptionOnScriptError(false);  
    System.out.println("// 5 设置超时 "); 
    webClient.getOptions().setTimeout(50000);  
    System.out.println("  允许绕过SSL认证 "); 
    webClient.getOptions().setUseInsecureSSL(true);
    System.out.println("  允许启动注册组件 "); 
    webClient.getOptions().setActiveXNative(true);
    HtmlPage page = webClient.getPage(url);
    System.out.println(" //等待JS驱动dom完成获得还原后的网页 "); 
    webClient.waitForBackgroundJavaScript(10000);  
    System.out.println("  网页内容 "); 
    System.out.println( page.asXml());  
    webClient.closeAllWindows();  

这是最基本的用法,同时实现了动态加载JS文件,返回新的试图的需求。

但是很可惜,目前为止,我仍在寻找其带参传递的方法。

我是用的场景是htmlunit+SpringMVC,需要通过htmlunit的URL连接访问对应页面,页面的数据是SpringMVC后台提供的,这就产生了一个矛盾:SpringMVC数据存入Session必须经过试图解析器渲染才能使用,而htmlunit只能返回URL对应的页面,所以我无法将htmlunit的URL指向SpringMVC的controller,也无法在同一个controller中使用SpringMVC和htmlunit获得数据。希望能有大神为我解惑。

此外htmlunit仍有更多强大的功能,比如获得页面表单、按钮对象并执行点击等操作以实现登录等功能,加载指定js文件以实现翻页等功能,还有其他很多的功能,但因为没有使用经验,就不贴出来了。

最后贴出所依赖的maven配置

 <dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.11.0</version>
</dependency>

<dependency>
  <groupId>xalan</groupId>
  <artifactId>xalan</artifactId>
  <version>2.7.1</version>
  <exclusions>
    <exclusion>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

 <dependency>
  <groupId>net.sourceforge.nekohtml</groupId>
  <artifactId>nekohtml</artifactId>
  <version>1.9.20</version>
  <exclusions>
    <exclusion>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.2</version>
</dependency>

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.2</version>
</dependency>

<dependency>
  <groupId>net.sourceforge.htmlunit</groupId>
  <artifactId>htmlunit-core-js</artifactId>
  <version>2.14</version>
</dependency>

<dependency>
  <groupId>net.sourceforge.cssparser</groupId>
  <artifactId>cssparser</artifactId>
  <version>0.9.13</version>
</dependency>

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-websocket</artifactId>
  <version>8.1.14.v20131031</version>
</dependency>

<dependency>
  <groupId>net.sourceforge.htmlunit</groupId>
  <artifactId>htmlunit</artifactId>
  <version>2.14</version>
</dependency>

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-htmlunit-driver</artifactId>
  <version>2.39.0</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
    </exclusion>
  </exclusions>
</dependency>

    <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-ie-driver</artifactId>
  <version>2.39.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-firefox-driver</artifactId>
  <version>2.39.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-chrome-driver</artifactId>
  <version>2.39.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.tmatesoft.svnkit</groupId>
  <artifactId>svnkit</artifactId>
  <version>1.7.8</version>
  <scope>test</scope>
</dependency>

猜你喜欢

转载自blog.csdn.net/weixin_39389850/article/details/78905412