相信你们都对爬虫很是熟悉,通常来讲,利用HttpClient发送请求并获取响应以得到想要提取的数据应该是最经常使用的方法。最近工做中频繁使用了Selenium,在本文中,咱们将使用Selenium和POI(读写Excel)来完成一个入门级的自动化程序,源码地址见附录。java
使用Maven建立工程,引入Selenium和POI依赖git
1.1 下载Maven,配置环境变量web
Windows和Mac将Maven目录地址写入path便可,具体步骤可百度,Google,十分常见。chrome
1.2 在IDEA中配置Mavenapache
IDEA自带Maven可能版本非最新,建议自行引入本地最新版本。bash
1.3 建立工程框架
建立工程时只要使用最基础的模板,也就是直接点击next。 ide
1.4 在mvnrepository.com搜索Selenium,POI和POI-ooxml依赖,将其引入pom.xml,并在右下角点击import change,最终pom.xml加入内容以下:spa
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
复制代码
下载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 page
public void setUp() {
//Go to youdao.com
driver.get(YOUDAO_HOME_URL);
driver.manage().window().maximize();
//Go to the main search page
driver.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 translation
public String searchword(String s) {
//Find the input element, input the word and click the button
WebElement 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 div
String 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 workbook
Workbook workbook = new XSSFWorkbook(fis);
fis.close();
return workbook;
}
复制代码
4.3 编写searchWord方法
调用Search类的searchWord进行搜索,而后将获取到的String写入Excel,代码以下:
//Search the word and write down
public void searchWord(Workbook workbook, int columnIndex, int count) {
//Initialize driver
WebDriver driver = new ChromeDriver();
Search search = new Search(driver);
search.setUp();
//Search for all words in one column and print to another column
for (int i = 0; i < count; i++) {
//Get value of the cell and get the translation through search method
Sheet 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 column
Cell 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 change
public void save(Workbook workbook) throws IOException {
FileOutputStream outputStream = new FileOutputStream(path);
workbook.write(outputStream);
outputStream.close();
workbook.close();
}
复制代码
编写main方法,运行程序
编写入口方法,代码以下:
//Entrance
public 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,感谢阅读。