关于在selenium 中 webdriver 截图操作

本文介绍如何利用Selenium WebDriver进行网页自动化操作,并通过示例代码演示了如何使用ChromeDriver打开百度首页并截取页面图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package prictce;

import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.sun.jna.platform.FileUtils;

public class baidu {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");  //启动驱动
WebDriver driver= new ChromeDriver(); //谷歌的驱动
driver.manage().window().maximize(); //讲窗口最大化
driver.get("http://www.baidu.com"); //打开百度

File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);    //讲截取的图片以文件的形式返回
try {
org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot.png"));     //使用copyFile()方法保存获取到的截图文件
} catch (IOException e) {

e.printStackTrace();
}
driver.quit();
}
}

转载于:https://www.cnblogs.com/yushengaqingzhijiao/p/7560290.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值