概述
前面的一篇博文详细介绍了单个控件抓取的设计思路&逻辑以及使用方法,本文将详述批量控件抓取功能。
批量抓取:打开一个web页面,遍历页面上所有能被抓取的元素,获得每个元素的iframe、和三个唯一定位控件的xpath,以及被抓取元素的缩略展示,供使用者勾选,并生成基于LazyUI框架的Java代码,解决整个页面元素的获取问题。
下载:
http://download.csdn.net/detail/kaka1121/9571942
拖拽安装到chrome即可右键使用
工具由来和设计思路在上篇博文中已有详细描述,此文中不再赘述。
http://blog.csdn.net/kaka1121/article/details/51878346
使用:
以百度首页为例,页面长这样:
点击右键
得到的结果如下(前7个):
简单来说,批量获取就是把每个控件单个获取的结果,以表格形式呈现,并且对获取到的控件做一个直观的展示(“控件展示”列),以便用户勾选需要的控件,生成代码。
如上图所示:
第一行为百度搜索框的抓取结果,
控件类型为“text”,
默认勾选生成代码,因为可以用id唯一定位,
给出了三个可对其唯一定位的xpath:
//input[@id='kw']
//input[@name='wd']
//input[contains(@class,'s_ipt')]控件名称和描述都默认取其id,可自行修改。
点击全选,可以将工具抓取到的所有控件勾选,(能用ID或name唯一定位的,默认勾选“生成代码”)
也可以在xpath列勾选抓取到的xpath,可以把那些不稳定的xpath勾选掉,
在最下面填写pagebean的包名和类名后,会生成一个.java文件,包含了bean层所有的代码,直接拷贝到工程中即可使用。
package bean; import lazy.test.ui.annotations.*; import lazy.test.ui.beans.PageBean; import lazy.test.ui.controls.*; import lazy.test.ui.browser.BrowserEmulator; public class baidu extends PageBean { @Xpath(xpath={"//input[@id='kw']", "//input[@name='wd']", "//input[contains(@class,'s_ipt')]"}) @Frame(frame="") @Description(description="kw") public Text kw; @Xpath(xpath={"//input[@id='su']", "//input[contains(@value,'百度一下')]", "//input[contains(@class,'bg s_btn')]"}) @Frame(frame="") @Description(description="su") public Click su; @Xpath(xpath={"//a[@name='ime_hw']", "//a[contains(text(),'手写')]", "/html/body/div[3]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[1]/a"}) @Frame(frame="") @Description(description="imehw") public Click imehw; @Xpath(xpath={"//a[@name='ime_py']", "//a[contains(text(),'拼音')]", "/html/body/div[3]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[2]/a"}) @Frame(frame="") @Description(description="imepy") public Click imepy; @Xpath(xpath={"//a[@name='ime_cl']", "//a[contains(text(),'关闭')]", "/html/body/div[3]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[4]/a"}) @Frame(frame="") @Description(description="imecl") public Click imecl; @Xpath(xpath={"//a[@name='tj_trnuomi']", "//a[contains(text(),'糯米')]", "/html/body/div[3]/div[1]/div/div[3]/a[1]"}) @Frame(frame="") @Description(description="tjtrnuomi") public Click tjtrnuomi; @Xpath(xpath={"//a[@name='tj_trnews']", "/html/body/div[3]/div[1]/div/div[3]/a[2]"}) @Frame(frame="") @Description(description="tjtrnews") public Click tjtrnews; @Xpath(xpath={"//a[@name='tj_trhao123']", "//a[contains(text(),'hao12')]", "/html/body/div[3]/div[1]/div/div[3]/a[3]"}) @Frame(frame="") @Description(description="tjtrhao123") public Click tjtrhao123; @Xpath(xpath={"//a[@name='tj_trmap']", "/html/body/div[3]/div[1]/div/div[3]/a[4]"}) @Frame(frame="") @Description(description="tjtrmap") public Click tjtrmap; @Xpath(xpath={"//a[@name='tj_trvideo']", "/html/body/div[3]/div[1]/div/div[3]/a[5]"}) @Frame(frame="") @Description(description="tjtrvideo") public Click tjtrvideo; @Xpath(xpath={"//a[@name='tj_trtieba']", "/html/body/div[3]/div[1]/div/div[3]/a[6]"}) @Frame(frame="") @Description(description="tjtrtieba") public Click tjtrtieba; @Xpath(xpath={"//div[@id='u1']//a[@name='tj_login']", "//a[@name='tj_trnuomi']/parent::div/a[7]", "/html/body/div[3]/div[1]/div/div[3]/a[7]"}) @Frame(frame="") @Description(description="tjlogin") public Click tjlogin; @Xpath(xpath={"//div[@id='u1']//a[@name='tj_settingicon']", "//a[@name='tj_trnuomi']/parent::div/a[8]", "/html/body/div[3]/div[1]/div/div[3]/a[8]"}) @Frame(frame="") @Description(description="tjsettingicon") public Click tjsettingicon; @Xpath(xpath={"//a[@name='tj_briicon']", "//a[contains(text(),'更多产品')]", "//a[contains(@class,'bri')]"}) @Frame(frame="") @Description(description="tjbriicon") public Click tjbriicon; @Xpath(xpath={"//a[@name='tj_mp3']", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[1]"}) @Frame(frame="") @Description(description="tjmp3") public Click tjmp3; @Xpath(xpath={"//a[@name='tj_img']", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[2]"}) @Frame(frame="") @Description(description="tjimg") public Click tjimg; @Xpath(xpath={"//a[@name='tj_zhidao']", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[3]"}) @Frame(frame="") @Description(description="tjzhidao") public Click tjzhidao; @Xpath(xpath={"//a[@name='tj_wenku']", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[4]"}) @Frame(frame="") @Description(description="tjwenku") public Click tjwenku; @Xpath(xpath={"//a[@name='tj_bang']", "//a[contains(text(),'风云榜')]", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[5]"}) @Frame(frame="") @Description(description="tjbang") public Click tjbang; @Xpath(xpath={"//a[@name='tj_tuiguang']", "//a[contains(text(),'百度推广')]", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/a[6]"}) @Frame(frame="") @Description(description="tjtuiguang") public Click tjtuiguang; @Xpath(xpath={"//a[@name='tj_more']", "//a[contains(text(),'全部产品>')]", "/html/body/div[3]/div[1]/div/div[4]/div/div[2]/div[1]/div/div/a"}) @Frame(frame="") @Description(description="tjmore") public Click tjmore; @Xpath(xpath={"//a[@id='setf']", "//a[contains(@style,'display: inline;')]", "/html/body/div[3]/div[3]/div/div[2]/p[1]/a[2]"}) @Frame(frame="") @Description(description="setf") public Click setf; public baidu(BrowserEmulator be) { super(be); } }
总结
使用此插件,可以快速地完成控件的获取,以及bean层代码(根据8种不同控件类型,提供其特异的方法)的自动生成,几乎完全释放控件获取的工作量。