selenium web driver 实现截图功能
在验证某些关键步骤时,需要截个图来记录一下当时的情况
Webdriver截图时,需要引入
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot;
截图方法
public static void snapshot(TakesScreenshot drivername, String filename) { // this method will take screen shot ,require two parameters ,one is driver name, another is file name String currentPath = System.getProperty("user.dir"); //get current work folder System.out.println(currentPath); File scrFile = drivername.getScreenshotAs(OutputType.FILE); // Now you can do whatever you need to do with it, for example copy somewhere try { System.out.println("save snapshot path is:"+currentPath+"/"+filename); FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename)); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Can't save screenshot"); e.printStackTrace(); } finally { System.out.println("screen shot finished"); } }
以下任务:
1.使用selenium打开百度,截图;
2.输入selenium关键字,截图;
3.搜索 并打开 selenium的百度百科,截图;
具体代码如下:
1 package baidu; 2 3 4 5 import java.io.File; 6 import java.io.IOException; 7 8 9 10 import org.apache.commons.io.FileUtils; 11 12 import org.openqa.selenium.By; 13 import org.openqa.selenium.OutputType; 14 import org.openqa.selenium.TakesScreenshot; 15 import org.openqa.selenium.WebDriver; 16 //import org.openqa.selenium.WebDriver.Navigation; 17 import org.openqa.selenium.WebElement; 18 import org.openqa.selenium.chrome.ChromeDriver; 19 20 21 22 23 public class selenium { 24 25 26 27 public static void snapshot(TakesScreenshot drivername, String filename) 28 { 29 // this method will take screen shot ,require two parameters ,one is driver name, another is file name 30 31 32 File scrFile = drivername.getScreenshotAs(OutputType.FILE); 33 // Now you can do whatever you need to do with it, for example copy somewhere 34 try { 35 System.out.println("save snapshot path is:E:/"+filename); 36 FileUtils.copyFile(scrFile, new File("E:\\"+filename)); 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 System.out.println("Can't save screenshot"); 40 e.printStackTrace(); 41 } 42 finally 43 { 44 System.out.println("screen shot finished"); 45 } 46 } 47 48 public static void main (String [] args) throws InterruptedException 49 { 50 51 52 String URL="http://www.baidu.com"; 53 System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe"); 54 WebDriver driver = new ChromeDriver(); 55 driver.get(URL); 56 //max size the browser 57 driver.manage().window().maximize(); 58 /* 59 Navigation navigation = driver.navigate(); 60 navigation.to(URL);*/ 61 Thread.sleep(2000); 62 snapshot((TakesScreenshot)driver,"open_baidu.png"); 63 //WebElement reg=driver.findElement(By.name("tj_reg")); 64 //reg.click(); 65 // WebElement keyWord = driver.findElement(By.id("kw1")); 66 67 //find the element 68 WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']")); 69 keyWord.clear(); 70 //send key words 71 keyWord.sendKeys("Selenium"); 72 Thread.sleep(3000); 73 snapshot((TakesScreenshot)driver,"input_keyWord.png"); 74 75 76 77 WebElement submit = driver.findElement(By.id("su1")); 78 79 System.out.println(submit.getLocation()); 80 submit.click(); 81 //System.out.println(driver.getWindowHandle()); 82 Thread.sleep(5000); 83 84 // System.out.println(driver.getPageSource()); 85 86 String pageSource=driver.getPageSource(); 87 // System.out.println(pageSource); 88 //WebElement link =driver.findElement(By.xpath(SELENIUM_LINK)); 89 WebElement link =driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")); //*[@id="1"]/h3/a 90 link.click(); 91 Thread.sleep(5000); 92 driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]); 93 94 //get page title 95 System.out.println(driver.getTitle()); 96 Thread.sleep(5000); 97 // navigation.back(); 98 snapshot((TakesScreenshot)driver,"open_bake.png"); 99 System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl()); 100 101 102 103 driver.quit(); 104 105 106 } 107 108 }
在百度搜索结果中,拿到你想要的elements,可以使用浏览器的查看元素,通过xpath方法获取
运行此代码后截图效果如下:
console输出:
Starting ChromeDriver (v2.9.248315) on port 33834
save snapshot path is:E:/open_baidu.png
screen shot finished
save snapshot path is:E:/input_keyWord.png
screen shot finished
(858, 179)
Selenium_百度百科
save snapshot path is:E:/open_bake.png
screen shot finished
Selenium_百度百科
http://baike.baidu.com/subview/478050/6464537.htm?fr=aladdin
转载请注明出处:http://www.cnblogs.com/tobecrazy/

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-03-13 常用排序算法-冒泡排序