1.selenium截图
import com.google.common.io.Files;
import org.openqa.selenium.By;
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.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.IOException;
public class ChromeTest {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","D:\\RICH_WOMAN\\driver\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.setBinary("D:\\RICH_WOMAN\\browser\\chrome.exe");
driver.get("https://www.baidu.com/");
driver.manage().window().maximize();
driver.findElement(By.id("kw")).sendKeys("how to rich");
driver.findElement(By.id("su")).click();
// selenium提供了截图的功能,分别是接口是TakesScreenshot和类RemoteWebDriver
File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
//利用FileUtils工具类的copy()方法保存getScreenshotAs()返回的文件对象。
//看到网上有使用File.copyFile()方法,我这里测试的结果需要使用copy()方法
Files.copy(scrFile, new File("d:\\RICH_WOMAN\\img\\screenfile.png"));
Files.copy(file, new File("d:\\RICH_WOMAN\\img\\file.png"));
} catch (IOException e) {
// 异常处理
e.printStackTrace();
}
driver.quit();
}
}
2.maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>RICH_WOMAN</artifactId>
<groupId>LOCAL</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.6.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>