java实现网页截图

时间:2020-12-18 07:18:09

使用工具

java+selenium+phantomjs /chromedriver /firefox

1.分别是 phantomjs插件 google截图插件 和 firefox火狐浏览器截图插件
2. selenium工具 是对页面进行点击操作后在截图 需要用到的工具。

插件的使用方法几乎一致

上代码:1.使用phantomjs截图

     public static String getPicByPhantomjs(Map<String ,String> map,String pic,File sf){
logger.warn("使用phantomjs截图链接:"+map.get("url"));
//定义图片存储路径
DesiredCapabilities dcaps = null;
PhantomJSDriver driver = null;
String picName = null;
try {
//设置必要参数
dcaps = new DesiredCapabilities();
//ssl证书支持
dcaps.setCapability("acceptSslCerts", true);
//截屏支持
dcaps.setCapability("takesScreenshot", true);
//css搜索支持
dcaps.setCapability("cssSelectorsEnabled", true);
//js支持
dcaps.setJavascriptEnabled(true);
//驱动支持(第二参数表明的是你的phantomjs引擎所在的路径)
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"D:/phantomjs/phantomjs-2.1.1-windows/bin/phantomjs.exe");
// "F:/test/phantomjs-2.1.1-windows/bin/phantomjs.exe");
//创建*面浏览器对象
driver = new PhantomJSDriver(dcaps);
long start = System.currentTimeMillis();
// driver.get(url);
driver.get(map.get("url"));
//设置隐性等待(作用于全局)
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
Thread.sleep(5* 1000);
if(!"".equals(map.get("str")) && map.get("str")!=null){
if(ElementExist(driver,By.className(map.get("str")))){
WebElement inputBox = driver.findElement(By.className(map.get("str")));
Actions action = new Actions(driver);
action.click(inputBox).build().perform();
//元素点击 后等待加载
Thread.sleep(2 * 1000);
}
} JavascriptExecutor js = driver;
//页面下滑10次,每次下滑加载2s
for (int i = 0; i < 10; i++) {
js.executeScript("window.scrollBy(0,1000)");
//睡眠2s等js加载完成
Thread.sleep(2 * 1000);
}
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(2000);
//利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象
FileUtils.copyFile(srcFile, new File(sf.getPath()+File.separator+pic));
Thread.sleep(2000);
Thumbnails.of(sf.getPath()+File.separator+pic).scale(1.0f).toFile(sf.getPath()+File.separator+pic);
driver.close();
driver.quit();
picName = pic;
System.out.println("本次截图耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
// System.out.println("转换后的链接:"+map.get("url"));
} catch (Exception e) {
driver.close();
driver.quit();
picName = "";
logger.warn("使用phantomjs截图时:"+e.toString());
}
return picName;
}

使用 phantomjs截图

2.使用 chromedriver截图

     public static String getPicByChrom(Map<String ,String> map,String pic,File sf){

                 logger.warn("使用chromedriver截图链接:"+map.get("url"));
//定义图片存储路径
WebDriver driver = null ;
String picName = null;
try {
long start = System.currentTimeMillis();
String chromeDriverDir = "D:/chromedriver/chromedriver_win32/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverDir);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(map.get("url"));
//设置隐性等待(作用于全局)
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
Thread.sleep(5* 1000);
if(!"".equals(map.get("str")) && map.get("str")!=null){
if(ElementExist(driver,By.className(map.get("str")))){
WebElement inputBox = driver.findElement(By.className(map.get("str")));
Actions action = new Actions(driver);
action.click(inputBox).build().perform();
//元素点击 后等待加载
Thread.sleep(2 * 1000);
}
} // JavascriptExecutor js = (JavascriptExecutor) driver;
//页面下滑10次,每次下滑加载2s
// for (int i = 0; i < 1; i++) {
// js.executeScript("window.scrollBy(0,1000)");
// //睡眠2s等js加载完成
// Thread.sleep(2 * 1000);
// }
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(2000);
//利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象
FileUtils.copyFile(srcFile, new File(sf.getPath()+File.separator+pic));
Thread.sleep(2000);
Thumbnails.of(sf.getPath()+File.separator+pic).scale(1.0f).toFile(sf.getPath()+File.separator+pic);
driver.close();
driver.quit();
picName = pic;
System.out.println("本次截图耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
// System.out.println("转换后的链接:"+map.get("url"));
} catch (Exception e) {
driver.close();
driver.quit();
picName = "";
logger.warn("使用chromedriver截图时:"+e.toString());
}
return picName;
}

chromedriver截图使用

注 :

1.phantomjs截图会创建*面浏览器进行截图,可截取网页长图 (推荐使用)
2.chromedriver 截图工具只能截取当前浏览器可见区域长度(调用浏览器打开url地址,插件版本需要与chrom浏览器版本一致)

3.Thumbnails 为图片压缩工具