写在最前
相信大家都对爬虫非常熟悉,一般来说,利用HttpClient发送请求并获取响应以获得想要提取的数据应该是最常用的方法。最近工作中频繁使用了Selenium,在本文中,我们将使用Selenium和POI(读写Excel)来完成一个入门级的自动化程序,源码地址见附录。
步骤一览
- 使用Maven创建工程,引入Selenium和POI依赖
- 下载ChromeDriver并配置环境变量
- 编写Selenium查词脚本
- 读写Excel并保存
- 编写main方法,运行程序
现在开始
-
使用Maven创建工程,引入Selenium和POI依赖
1.1 下载Maven,配置环境变量
Windows和Mac将Maven目录地址写入path即可,具体步骤可百度,Google,十分常见。
1.2 在IDEA中配置Maven
IDEA自带Maven可能版本非最新,建议自行引入本地最新版本。
1.3 创建工程
创建工程时只要使用最基础的模板,也就是直接点击next。
1.4 在mvnrepository.com搜索Selenium,POI和POI-ooxml依赖,将其引入pom.xml,并在右下角点击import change,最终pom.xml加入内容如下:
复制代码org.seleniumhq.selenium selenium-java 3.14.0 org.apache.poi poi 4.0.0 org.apache.poi poi-ooxml 4.0.0 -
下载ChromeDriver并配置环境变量(三选一)
2.1 在镜像站下载ChromeDriver,配置环境变量
自行手动下载ChromeDriver后如不配置环境变量,需在代码中加上System.setProperty("webdriver.chrome.driver",path); 其中path是你的driver路径。
2.2 Windows使用choco install直接安装
2.3 Mac使用brew install cask直接安装
-
编写Selenium查词脚本
3.1 创建Search类,编写setUp方法 在setUp中,首先需要初始化WebDriver,然后访问到有道首页,搜索test点击确定并跳转至搜索页,注意在driver访问此页面时会弹出广告,需要一行代码来抓取关闭链接关掉广告,代码如下:
//Direct to YoudaoDic homepage, land in the main search pagepublic void setUp() {//Go to youdao.comdriver.get(YOUDAO_HOME_URL);driver.manage().window().maximize();//Go to the main search pagedriver.findElement(By.id(INPUT_HOME_ID)).sendKeys("test");driver.findElement(By.xpath(SEARCH_HOME_XPATH)).click();driver.findElement(By.xpath(CLOSE_BTN)).click();} 复制代码3.2 编写searchWord脚本方法
searchWord方法需要传入你要搜索的单词,然后抓取搜索框,输入后点击确认。这时你将获得搜索详情的页面,其中你需要抓取中文翻译的div并且获取其中文字,代码如下:
//Search word and get the translationpublic String searchword(String s) {//Find the input element, input the word and click the buttonWebElement input_search = driver.findElement(By.id(INPUT_SEARCH));input_search.clear();input_search.sendKeys(s);driver.findElement(By.xpath(SEARCH_BTN_XPATH)).click();//Get the text inside translation divString result = driver.findElement(By.className(TRANSLATION_CLASS)).getText();return result;} 复制代码 -
读写Excel并保存
4.1 创建Excel文件并写入单词
新建一个Excel,然后在最左边第一列填入一些单词,注意,不要有空行,本文代码中没有带异常处理,空行会报错。
4.2 编写Excelio类,编写read方法
利用poi框架,与普通文件读写异曲同工,代码如下:
public Workbook read(int columnIndex, int count) throws IOException {FileInputStream fis = null;fis = new FileInputStream(new File(path));//Input and save as a xlsx workbookWorkbook workbook = new XSSFWorkbook(fis);fis.close();return workbook; } 复制代码4.3 编写searchWord方法
调用Search类的searchWord进行搜索,然后将获取到的String写入Excel,代码如下:
//Search the word and write downpublic void searchWord(Workbook workbook, int columnIndex, int count) {//Initialize driverWebDriver driver = new ChromeDriver();Search search = new Search(driver);search.setUp();//Search for all words in one column and print to another columnfor (int i = 0; i < count; i++) {//Get value of the cell and get the translation through search methodSheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(i);Cell cell = row.getCell(columnIndex);String results = search.searchword(cell.getStringCellValue());//Write the translation to another columnCell temp = row.createCell(columnIndex+1);temp.setCellValue(results);//Set the new column as "Wrap Text"CellStyle cellStyle = workbook.createCellStyle();cellStyle.setWrapText(true);temp.setCellStyle(cellStyle);sheet.setColumnWidth(1,31*256);} 复制代码4.4 编写save方法
使用FileOutputStream,保存Excel,代码如下:
//Save the changepublic void save(Workbook workbook) throws IOException {FileOutputStream outputStream = new FileOutputStream(path);workbook.write(outputStream);outputStream.close();workbook.close();} 复制代码 -
编写main方法,运行程序
编写入口方法,代码如下:
//Entrancepublic static void main(String[] args) throws IOException {Excelio excelio = new Excelio("src/main/resources/wordlist/test.xlsx");Workbook workbook = excelio.read(0, 30);excelio.searchWord(workbook,0,30);excelio.save(workbook);}
复制代码 结果展示
Excel
结语
本文是为Selenium的入门而设计,后续作者将在此基础上,将Selenium结合JavaWeb,更新自动化WebApp的文章,敬请期待。如有疑问欢迎留言交流,欢迎issue和PR,感谢阅读。
附录
源码地址:gitee.com/daniel_ddd/…