代码改变世界

selenium webdriver 截屏操作

2016-12-05 21:52  清风软件测试开发  阅读(1233)  评论(0编辑  收藏  举报

有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时,

我们就需要对正在处理的页面进行截屏!

未经作者允许,禁止转载!

package test_wait20161205;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class screen_shot20161205 {

    public static void main(String[] args) throws InterruptedException, IOException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chorme.driver", "C:\\Training\\Automation\\Software\\drivers\\chromedriver_win32\\chromedriver.exe");和上面一行是连着的
        WebDriver driver=new ChromeDriver();
        driver.get("https://www.hao123.com/");
        Thread.sleep(1000);
        
        File screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);-------------------------截屏操作        
        FileUtils.copyFile(screenshot, new File("C:\\Users\\chenjia\\Desktop\\screenshots1.jpg"));----------保存图片
        
        System.out.println("将截屏的图片保存在桌面");
                
    }

}