51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1726|回复: 3

[测试开发班] 截图内容为空白

[复制链接]

该用户从未签到

发表于 2019-5-10 11:17:13 | 显示全部楼层 |阅读模式
本帖最后由 辣舞辣舞test 于 2019-5-10 11:18 编辑

ImageIO.write无法写入jepg格式的图片,改成png格式,截的图片是空白的。
package com.ctrip.trains;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import org.apache.commons.io.FileUtils;import org.apache.commons.io.output.ByteArrayOutputStream;import org.openqa.selenium.By;import org.openqa.selenium.OutputType;import org.openqa.selenium.Point;import org.openqa.selenium.TakesScreenshot;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class Screenshot {        public static void main(String[] args) throws IOException, InterruptedException {                                //强制类型转换                /*                int a=(int)3.5;                System.out.println(a);                 */                System.setProperty("webdriver.chrome.driver", "/Users/yuxiaojun/Downloads/software/Eclipse/eclipse-workspace/se_2018/Files/chromedriver");                WebDriver driver=new ChromeDriver();                //浏览器窗口最大化                driver.manage().window().maximize();                driver.get("https://user.qunar.com/passport/login.jsp");                Thread.sleep(5000);                //截图,结果以文件格式保存                /*                File f=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);                //将截图复制至指定路径                FileUtils.copyFile(f,new File("/Users/yuxiaojun/Downloads/software/Eclipse/eclipse-workspace/se_2018/Files/1.jpeg"));                */                                WebElement image=driver.findElement(By.id("vcodeImg"));                                BufferedImage pic=imageElement(driver,image);                                ImageIO.write(pic, "png", new File("/Users/yuxiaojun/Downloads/software/Eclipse/eclipse-workspace/se_2018/Files/7.png"));        }                //截图,结果以bytes类型保存在内存中        public static byte[] Screen(WebDriver driver) {                return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);        }                public static BufferedImage imageElement(WebDriver driver,WebElement ele) throws IOException {                //获取需要截图的元素的位置                Point location=ele.getLocation();                //从内存读取截图                BufferedImage im=ImageIO.read(new ByteArrayInputStream(Screen(driver)));                //从截图中获取指定位置的子图片                BufferedImage subim=im.getSubimage(location.getX(), location.getY(), ele.getSize().getWidth(), ele.getSize().getHeight());                return subim;        }        }
在学课程
Java Selenium3项目实战

回复

使用道具 举报

该用户从未签到

发表于 2019-5-10 11:37:23 | 显示全部楼层
1、确认下是哪个系统,苹果部分系统有截图是空白问题

2、去哪儿登录页面有时候会显示二维码,需要先点击下

3、测试下你的环境截其它元素截图
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2019-5-10 11:40:16 | 显示全部楼层
1.是苹果系统
3.截其他元素也是空白
估计是苹果系统的问题
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2019-5-10 11:44:14 | 显示全部楼层
有的版本苹果系统是这样的
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-28 16:35 , Processed in 0.064186 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表