HtmlUnit是java版本的浏览器,没有界面但是可以模拟任何浏览器操作。
下面示例访问htmlunit的官网,并打印网页的标题
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Try { public static void main (String args[]) throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); System.out.println(page.getTitleText()); webClient.closeAllWindows(); } }
输出: HtmlUnit – Welcome to HtmlUnit
HtmlUnit提供了getElementById和Xpath方式查找界面上的元素,如下示例:
HtmlDivision div=(HtmlDivision)page.getElementById("hed");
HtmlDivision div=(HtmlDivision)page.getByXPath("//div").get(0);
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10335.html