确定浏览器和驱动的大版本一致
我的:
浏览器版本: 126.0.6478.57
驱动版本: 126.0.6478.55
设置允许跨域
可以看到控制台提示:Only local connections are allowed.
代码添加:
options.addArguments("--remote-allow-origins=*");
可解决如下报错:
警告: Invalid Status code=403 text=Forbidden
java.io.IOException: Invalid Status code=403 text=Forbidden
..............
..............
org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:56812/devtools/browser/205629ab-1217-4904-b128-3ea96c6a7025
Build info: version: '4.1.4', revision: '535d840ee2'
System info: host: 'LAPTOP-1SOTANT0', ip: '172.26.2.200', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: driver.version: ChromeDriver
maven依赖版本别太高或太低
我使用4.10.0
没问题
使用较高版本4.21.0
时会报错:
信息: Detected dialect: W3C
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openqa/selenium/devtools/v123/v123CdpInfo has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
使用较低版本3.141.59
会报错:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lcom/google/common/collect/ImmutableMap;
selenium-java实现后端简单截图代码参考
public static void snapshotBySelenium(String url) {
// 根据系统来添加不同的驱动路径
String os = System.getProperty("os.name");
if (StrUtil.containsIgnoreCase(os, "windows")) {
System.setProperty("webdriver.chrome.driver", "D:\\Workspaces\\chromedriver\\chromedriver.exe");
} else {
// 只考虑Linux环境,需要下载对应版本的驱动后放置在绝对路径/usr/bin目录下
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
}
try {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--remote-allow-origins=*");
ChromeDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get(url);
String jsHeight = "return document.body.clientHeight";
long height = (long) driver.executeScript(jsHeight);
int k = 1;
int size = 500;
while (k * size < height) {
String jsMove = String.format("window.scrollTo(0,%s)", k * 500);
driver.executeScript(jsMove);
Thread.sleep(100);
height = (long) driver.executeScript(jsHeight);
k += 1;
}
// 通过执行脚本解决Selenium截图不全问题
long maxWidth = (long) driver.executeScript(
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);");
long maxHeight = (long) driver.executeScript(
"return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);");
Dimension targetSize = new Dimension((int)maxWidth, (int)maxHeight);
driver.manage().window().setSize(targetSize);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String strTime = sdf.format(date);
File img = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(img, new File(strTime + ".png"));
} catch (Exception e) {
e.printStackTrace();
}
}
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
</dependency>