博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
htmlunit入门
阅读量:7011 次
发布时间:2019-06-28

本文共 2106 字,大约阅读时间需要 7 分钟。

hot3.png

示例1:获取oschina网站的title
import  com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;import  com.gargoylesoftware.htmlunit.WebClient;import  com.gargoylesoftware.htmlunit.html.HtmlPage;     public   static   void  getHomeTile()  throws  FailingHttpStatusCodeException, MalformedURLException, IOException{         final  WebClient webClient  =   new  WebClient();         final  HtmlPage htmlPage  =  webClient.getPage( " http://www.oschina.net " );         System.out.println(htmlPage.getTitleText());         System.out.println(htmlPage.getTextContent());    }
可以看见打印出:开源中国 - 找到您想要的开源软件,分享和交流
示例二:获取一个特定的浏览器版本
public   void  homePage_Firefox()  throws  Exception {     final  WebClient webClient  =   new  WebClient(BrowserVersion.FIREFOX_2);     final  HtmlPage page  =  webClient.getPage( " http://htmlunit.sourceforge.net " );     assertEquals( " HtmlUnit - Welcome to HtmlUnit " , page.getTitleText());}
示例三:获取特定ID的DIV或者锚链接
public   void  getElements()  throws  Exception {     final  WebClient webClient  =   new  WebClient();     final  HtmlPage page  =  webClient.getPage( " http://some_url " );     final  HtmlDivision div  =  page.getHtmlElementById( " some_div_id " );     final  HtmlAnchor anchor  =  page.getAnchorByName( " anchor_name " );}
实例四:模拟页面提交一个form
public   void  submittingForm()  throws  Exception {     final  WebClient webClient  =   new  WebClient();     //  Get the first page      final  HtmlPage page1  =  webClient.getPage( " http://some_url " );     //  Get the form that we are dealing with and within that form,      //  find the submit button and the field that we want to change.      final  HtmlForm form  =  page1.getFormByName( " myform " );     final  HtmlSubmitInput button  =  form.getInputByName( " submitbutton " );     final  HtmlTextInput textField  =  form.getInputByName( " userid " );     //  Change the value of the text field      textField.setValueAttribute( " root " );     //  Now submit the form by clicking the button and get back the second page.      final  HtmlPage page2  =  button.click();}

转载于:https://my.oschina.net/liangtee/blog/106210

你可能感兴趣的文章
UVA 10056 What is the Probability ?
查看>>
DotNet4应用程序打包工具->升级版【三】宿主程序分析+全部源码下载
查看>>
WMI事件监控
查看>>
pku 2186 Popular Cows (tarjan缩点)
查看>>
c关键字控制语句运算符小结
查看>>
初学Oracle的笔记(1)——基础内容(实时更新中..)
查看>>
(转)Ogre 安装 配置 问题
查看>>
【转】清理浮动的全家
查看>>
ZJOI2006 物流运输trans
查看>>
WinForm编程数据视图之DataGridView浅析(续)
查看>>
vim中实现括号和引号自动补全
查看>>
linux下的块设备驱动(一)
查看>>
Git-Flow
查看>>
mysql 5.7 安装手册(for linux)
查看>>
7.1-7.31推荐文章汇总
查看>>
ubuntu16.4中安装samba服务
查看>>
IT Operations(IT 运营),运维的更价值化认识
查看>>
eclipse启动时虚拟机初始内存配置
查看>>
在Linux用户空间做内核空间做的事情
查看>>
Chapter 1 First Sight——29
查看>>