java web截屏_Selenium Webdriver实现截图功能的示例

前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot)。

废话不多说了,直接上代码

package com.wch;

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.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.WebDriverWait;

import com.sun.jna.platform.FileUtils;

public class TestTakesScreenshot {

public static void main(String[] args) {

System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

WebDriver driver = new FirefoxDriver();

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) {

// TODO Auto-generated catch block

e.printStackTrace();

}

driver.quit();

}

}

还有其他方法的话希望各位也能提供下,互相学习。

以上这篇Selenium Webdriver实现截图功能的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Selenium WebDriver 控制 Chrome 浏览器的完整示例代码,包括打开网页、查找元素、填写表单、点击按钮、截屏等操作: ```vb.net Imports OpenQA.Selenium Imports OpenQA.Selenium.Chrome Imports System.Drawing.Imaging ' 创建 ChromeDriver 实例 Dim options As ChromeOptions = New ChromeOptions() options.AddArgument("--start-maximized") Dim driver As IWebDriver = New ChromeDriver(options) ' 打开网页 driver.Navigate().GoToUrl("https://www.baidu.com") ' 查找元素 Dim searchBox As IWebElement = driver.FindElement(By.Name("wd")) ' 填写表单 searchBox.SendKeys("Selenium WebDriver") ' 点击按钮 searchBox.Submit() ' 获取搜索结果 Dim searchResults As ReadOnlyCollection(Of IWebElement) = driver.FindElements(By.CssSelector("div.result.c-container")) For Each result As IWebElement In searchResults Console.WriteLine(result.FindElement(By.CssSelector("h3")).Text) Next ' 截屏 Dim screenshot As Screenshot = CType(driver, ITakesScreenshot).GetScreenshot() screenshot.SaveAsFile("screenshot.png", ImageFormat.Png) ' 关闭浏览器 driver.Quit() ``` 上面的代码会打开 Chrome 浏览器,并在最大化窗口后访问百度网页。然后它会在搜索框中输入“Selenium WebDriver”,并点击搜索按钮。最后,它会获取搜索结果,并输出每个结果的标题。代码还包括截屏操作,可以将浏览器当前界面保存为图片。 您可以使用类似的方式控制 Chrome 浏览器进行各种自动化测试和操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值