java+selenium+截图

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值