windows安装chromedriver.exe,谷歌浏览器驱动。(注意要和本机的chrome版本一致)
System.setProperty ("webdriver.chrome.driver" , "G:\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "...";
webDriver.get(url);
// 截图
File screenshot = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
BufferedImage eleScreenshot= fullImg.getSubimage(0, 0,1366, 600);
int width = eleScreenshot.getWidth();
int height = eleScreenshot.getHeight();
//压缩图片0.2倍
width = parseDoubleToInt(width * 0.2);
height = parseDoubleToInt(height * 0.2);
Image image = eleScreenshot.getScaledInstance(width, height,
Image. SCALE_SMOOTH);
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage. TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO. write(outputImage , "png" , os);
System. out.println(os.toByteArray(). length);
//上传到服务器
JSONObject jsResult = AttachFactory.saveAttach("http://pic.farmeasy.cn",count + ".png", os.toByteArray(),
AttachFactory. IMAGE_ATTACH_TYPE, AttachFactory.E_YOUPIN_BUSINESS_TYPE , AttachFactory.PERSONS_DIRECTORY_TYPE ,1);
if(jsResult.getString( "invoke_result").equals( "INVOKE_SUCCESS")){
String filepath = JSONObject.fromObject(jsResult.getString( "url")).getString( "fileUrl");
System. out.println(count + "成功" + baseId + "-->" + filepath);
} else{
System. out.println(count + "失败" + baseId);
}