springboot Selenium Chrome ChromeDriver 实现网页截屏
所属分类 java
浏览量 37
springboot 2.7.10
selenium-java 3.141.0
which google-chrome
/usr/bin/google-chrome
google-chrome --version
Google Chrome 131.0.6778.85
google-chrome --version
Google Chrome 131.0.6778.85
/usr/local/bin/chromedriver --version
ChromeDriver 131.0.6775.0 (e16e621006797934c49f419e0850be08ad7e8d23-refs/branch-heads/6775@{#1})
application.properties
webdriver.chrome.driver.path=/usr/local/bin/chromedriver
commons-io
commons-io
2.11.0
org.seleniumhq.selenium
selenium-java
3.141.0
org.seleniumhq.selenium
selenium-firefox-driver
org.seleniumhq.selenium
selenium-chromium-driver
org.seleniumhq.selenium
selenium-edge-driver
org.seleniumhq.selenium
selenium-ie-driver
org.seleniumhq.selenium
selenium-opera-driver
org.seleniumhq.selenium
selenium-safari-driver
org.seleniumhq.selenium
selenium-api
3.141.0
org.seleniumhq.selenium
selenium-chrome-driver
3.141.0
org.seleniumhq.selenium
selenium-remote-driver
3.141.0
org.seleniumhq.selenium
selenium-support
3.141.0
com.squareup.okhttp3
okhttp
3.11.0
org.apache.commons
commons-lang3
webdriveradmin.service.SeleniumService
public class SeleniumService {
@Value("${webdriver.chrome.driver.path}")
private String chromeDriverPath = "";
private volatile ChromeDriver chromeDriver;
private static final long MAX_SLEEP_TIME = 30 * 1000L;
public String getChromeDriverPath() {
return chromeDriverPath;
}
private ChromeDriver getOrCreateChromeDriver() {
ChromeDriver chromeDriverTmp = chromeDriver;
if (chromeDriverTmp != null) {
return chromeDriverTmp;
}
chromeDriverTmp = getOrCreateChromeDriverSync();
return chromeDriverTmp;
}
private synchronized ChromeDriver getOrCreateChromeDriverSync() {
ChromeDriver chromeDriverTmp = chromeDriver;
if (chromeDriverTmp != null) {
return chromeDriverTmp;
}
chromeDriverTmp = createChromeDriver();
chromeDriver = chromeDriverTmp;
return chromeDriverTmp;
}
private ChromeDriver createChromeDriver() {
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
// 启用 JavaScript(默认情况下是启用的,但明确设置)
options.addArguments("--enable-javascript");
// 其他可选的 Chrome 选项
options.addArguments("--headless"); // 无头模式,不显示浏览器窗口
options.addArguments("--disable-gpu"); // 禁用 GPU 加速
options.addArguments("--window-size=1920,1080"); // 设置窗口大小
long start = System.currentTimeMillis();
ChromeDriver driver = new ChromeDriver(options);
long time = System.currentTimeMillis() - start;
return driver;
}
public File getScreenshotAsFile(String url, long waitTime, String expectTitle) {
ChromeDriver chromeDriver = null;
try {
chromeDriver.get(url);
doWait(chromeDriver, waitTime, expectTitle);
File file = ((TakesScreenshot) chromeDriver).getScreenshotAs(OutputType.FILE);
return file;
} finally {
// 不能close ,close之后 在访问 报错
// org.openqa.selenium.NoSuchSessionException: invalid session id
// closeDriver(chromeDriver);
}
}
public void getScreenshotAndSave(String destFile, String url, long waitTime, String expectTitle) throws Exception {
ChromeDriver chromeDriver = null;
try {
chromeDriver = getOrCreateChromeDriver();
chromeDriver.get(url);
doWait(chromeDriver, waitTime, expectTitle);
byte[] bytes = ((TakesScreenshot) chromeDriver).getScreenshotAs(OutputType.BYTES);
File f = new File(destFile);
FileUtils.writeByteArrayToFile(f, bytes);
} finally {
//closeDriver(chromeDriver);
}
}
public byte[] getScreenshotAsBytes(String url, long waitTime, String expectTitle) {
ChromeDriver chromeDriver = null;
try {
chromeDriver = getOrCreateChromeDriver();
chromeDriver.get(url);
doWait(chromeDriver, waitTime, expectTitle);
byte[] bytes = ((TakesScreenshot) chromeDriver).getScreenshotAs(OutputType.BYTES);
return bytes;
} finally {
//closeDriver(chromeDriver);
}
}
public String getScreenshotAsBase64(String url, long waitTime, String expectTitle) {
ChromeDriver chromeDriver = null;
try {
chromeDriver = getOrCreateChromeDriver();
chromeDriver.get(url);
doWait(chromeDriver, waitTime, expectTitle);
String base64OfPNG = ((TakesScreenshot) chromeDriver).getScreenshotAs(OutputType.BASE64);
return base64OfPNG;
} finally {
// closeDriver(chromeDriver);
}
}
private static void doWait(ChromeDriver driver, long waitTime, String expectTitle) {
if (waitTime <= 0) {
return;
}
if (StringUtils.isBlank(expectTitle)) {
doSleep(waitTime);
return;
}
final long start = System.currentTimeMillis();
while (true) {
final long now = System.currentTimeMillis();
final long dif = now - start;
// System.out.println("waitForDone_dif="+dif+","+ LocalDateTime.now());
if (Math.abs(dif) > waitTime) {
// System.out.println("waitForDone_break,"+ LocalDateTime.now());
break;
}
// final String url = driver.getCurrentUrl();
final String title = driver.getTitle();
//System.out.println("url="+url+",title="+title+","+ LocalDateTime.now());
// 标题匹配 直接 跳出
if (expectTitle.equals(title)) {
break;
}
doSleep(1000);
}
}
private static void doSleep(long time) {
if (time <= 0) {
return;
}
if (time > MAX_SLEEP_TIME) {
time = MAX_SLEEP_TIME;
}
try {
Thread.sleep(time);
} catch (Exception e) {
// do nothing
}
}
private void closeDriver(ChromeDriver driver) {
if (driver == null) {
return;
}
try {
driver.close();
} catch (Exception e) {
System.out.println("close_driver_error,driver=" + driver.toString());
}
}
public void close() {
ChromeDriver chromeDriverTmp = chromeDriver;
if (chromeDriverTmp == null) {
return;
}
chromeDriverTmp.close();
chromeDriver = null;
}
public void quit() {
ChromeDriver chromeDriverTmp = chromeDriver;
if (chromeDriverTmp == null) {
return;
}
chromeDriverTmp.quit();
chromeDriver = null;
}
public void reset() {
ChromeDriver chromeDriverTmp = chromeDriver;
if (chromeDriverTmp == null) {
return;
}
try {
chromeDriverTmp.quit();
} catch (Exception e) {
System.out.println("ChromeDriver_quit_error," + e);
}
chromeDriver = null;
}
}
webdriveradmin.controller.WebDriverController
http://127.0.0.1:8078/
http://127.0.0.1:8078/webdriver/hello
http://127.0.0.1:8078/webdriver/getChromeDriverPath
http://127.0.0.1:8078/webdriver/quit
http://127.0.0.1:8078/webdriver/getScreenshotAndSave?destFile=/Users/dugang/tmp/003.png&waitTime=0&expectTitle=&url=http://codefun007.xyz
http://127.0.0.1:8078/webdriver/getScreenshotAsBase64Img?waitTime=0&expectTitle=&url=http://codefun007.xyz
http://127.0.0.1:8078/webdriver/getScreenshotAsBase64Img?waitTime=0&expectTitle=&url=http://127.0.0.1:8078
https://gitee.com/dyyx/work2024/tree/master/demo/webdriveradmin
上一篇
下一篇
Java通过URL获取页面快照
Linux 下使用 Java PhantomJS 和 Selenium 进行网页截图
java Selenium Chrome ChromeDriver 访问指定网页并截屏
Quartz 2.3.2 使用
裸K price action 概述
Spring @Cacheable 注解