模拟浏览器自动化测试工具Selenium之七采集网页信息写入excel
生活随笔
收集整理的這篇文章主要介紹了
模拟浏览器自动化测试工具Selenium之七采集网页信息写入excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
功能:從網頁上采集信息寫入excel,有鼠標移動到相關元素代碼,參考如下:
package com.test;import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.openqa.selenium.By; import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities;import com.util.Logs;public class EBayCom {public static void main(String[] args) throws IOException {System.getProperties().setProperty("webdriver.chrome.driver","D:\\dev\\workspace\\ocweb\\libs\\chromedriver.exe"); WebDriver webDriver = new ChromeDriver();//打開excel表,準備采集入表//獲取excel文件String path="D:"+System.getProperty("file.separator")+"tmp"+System.getProperty("file.separator")+"uee.xls";POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(path));//得到Excel工作簿對象 HSSFWorkbook wb = new HSSFWorkbook(fs); //得到Excel工作表對象 HSSFSheet sheet = wb.getSheetAt(0);int lastrow=sheet.getLastRowNum(); HSSFRow row = sheet.createRow(lastrow+1);//第一行標題,第二行開始寫入//訪問網址try {webDriver.get("url");//訪問網址//標題WebElement eleItemTitle = webDriver.findElement(By.id("itemTitle")); String txtItemTitle=eleItemTitle.getText();HSSFCell cellItemTitle=row.createCell(0);cellItemTitle.setCellValue(txtItemTitle);//圖片WebElement elePic= webDriver.findElement(By.id("vi_main_img_fs")); List<WebElement> eleImgs= elePic.findElements(By.tagName("img"));int i=1;//第二列for(WebElement img:eleImgs){Actions action=new Actions(webDriver); //模擬鼠標操作 action.moveToElement(img); Thread.sleep(1000);//休息1秒WebElement eleBPic= webDriver.findElement(By.id("icImg")); String src=eleBPic.getAttribute("src");//downloadImage(src,String.valueOf(i));HSSFCell cellsrc = row.createCell(i);cellsrc.setCellValue(src);i++;} //Seller informationWebElement eleSeller= webDriver.findElement(By.id("mbgLink"));String txtSeller=eleSeller.getText();HSSFCell cellSeller = row.createCell(4);cellSeller.setCellValue(txtSeller);String herf=eleSeller.getAttribute("href");HSSFCell cellhref= row.createCell(5);cellhref.setCellValue(herf);//產品描述Item SpecificsWebElement eleItem=webDriver.findElement(By.id("desc_ifr"));//String spesrc=eleItem.getAttribute("src");//獲取iframe頁面網址webDriver.switchTo().frame(eleItem);//切換到iframeThread.sleep(1000);//休息1秒WebElement eleinfo=webDriver.findElement(By.id("desc"));HSSFCell cellspecies= row.createCell(6);cellspecies.setCellValue(eleinfo.getText());FileOutputStream out=new FileOutputStream(path);out.flush();wb.write(out);out.close();}catch (Exception e) { System.err.println( "Exception: " + e );}wb.close(); fs.close();webDriver.close();webDriver.quit();}public static void downloadImage(String Imageurl,String filename) throws IOException{URL url = new URL(Imageurl);//打開網絡輸入流DataInputStream dis = new DataInputStream(url.openStream());String newImageName="D://tmp//"+filename+".jpg";//建立一個新的文件FileOutputStream fos = new FileOutputStream(new File(newImageName));byte[] buffer = new byte[1024];int length;//開始填充數據while((length = dis.read(buffer))>0){fos.write(buffer,0,length);}dis.close();fos.close(); } }總結
以上是生活随笔為你收集整理的模拟浏览器自动化测试工具Selenium之七采集网页信息写入excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HtmlUnit采集页面信息加工并写入e
- 下一篇: HtmlUnit动态执行js函数