目录
1.介绍
2.结果效果
2.1大图
2.2细节小图
3.依赖
4.代码
4.1引入
4.2具体代码
4.3执行
5.Linux下安装chrome和chromedriver
5.1yum安装
5.2下载下来安装
5.3安装必要的库
6.安装chromedriver,下载、解压、加权限
7.liunx 配置 驱动可执行权限 解决 无法执行驱动问题
7.1问题
7.2解决方案(赋予可执行权限)
8.linux 配置 中文编码 解决截图乱码问题
8.1centos6
8.2centos7 8
org.seleniumhq.selenium
selenium-java
3.9.1
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.swing.JPanel;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
package com.superman.global.service;
/**
* 网页转图片
*
* @author yushen
*
*/
@Service
public class HtmlToImgPngUitl{
/**
* 网页转图片
*
*
* @param savePath 保存图片地址
* @param url 访问网页地址
*
* @return boolean
*/
public boolean saveHtml(String savePath,String url){
// 谷歌驱动地址
String path ="";
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){ //微软系统
path+="d:/test/999/chromedriver.exe";
}else{//linux系统
path+="/lspt/bigdatapro/chromedriver/chromedriver";
}
File file = new File(path);// path: chromedirver.exe文件路径
ChromeDriverService service =new ChromeDriverService.Builder().usingDriverExecutable(file).usingAnyFreePort().build(); // 新建service 方便后面关闭chromedriver
WebDriver driver = null;
try {
service.start(); // 开启服务
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--no-sandbox");//无头浏览器
chromeOptions.addArguments("--disable-gpu");//无界面
chromeOptions.addArguments("lang=zh_CN.UTF-8");
//设置必要参数
// DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl证书支持
chromeOptions.setCapability("acceptSslCerts", true);
//截屏支持
chromeOptions.setCapability("takesScreenshot", true);
//css搜索支持
chromeOptions.setCapability("cssSelectorsEnabled", true);
//生成无头浏览器
driver = new ChromeDriver(service, chromeOptions);
//设置隐性等待
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
long start = System.currentTimeMillis();
//打开页面
driver.get(url);
// 获取页面尺寸
Long width = (Long) ((ChromeDriver) driver).executeScript("return Math.max(document.body.scrollWidth, " +
"document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement" +
".scrollWidth, document.documentElement.offsetWidth);");
Long height = (Long) ((ChromeDriver) driver).executeScript("return Math.max(document.body.scrollHeight, " +
"document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement" +
".scrollHeight, document.documentElement.offsetHeight);");
// 重新设置页面尺寸
Dimension dimension = new Dimension(width.intValue()+100, height.intValue());
driver.manage().window().setSize(dimension);
Thread.sleep(3000L);
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(2000L);
FileCopyUtils.copy(srcFile, new File(savePath)); // 复制文件 savePath:图片保存路径
System.out.println("图片生成完毕,耗时:{}ms"+ (System.currentTimeMillis() - start));
return true;
} catch (Exception e) {
System.out.println("保存异常,e="+ e.getMessage());
} finally {
// 关闭服务
driver.quit();
service.stop();
}
return false;
}
}
public static void main(String[] args) {
System.out.println("start");
new HtmlToImgPngUitl().saveHtml(
"d:/test/999/00aaaaaaaaaaa2.png"
,"https://blog.csdn.net/weixin_42749765");
System.out.println("end");
}
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
我没装上,但也无伤大雅哈哈
> wget http://npm.taobao.org/mirrors/chromedriver/
> unzip chromedriver_linux64.zip
> chmod +x /usr/bin/chromedriver
哦了
chmod a+x chromedriver
yum -y install fonts-chinese
yum groupinstall "fonts"
然后重启即可,我之前没重启也ok ,不得瑟哈哈
然后截图就是中文不乱吗喽
ok
持续更新