目錄
7.liunx 設定 驅動可執行許可權 解決 無法執行驅動問題
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>
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
持續更新