首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenide和Firefox记录错误的屏幕截图

问Selenide和Firefox记录错误的屏幕截图
EN

Stack Overflow用户
提问于 2017-09-08 16:39:15
回答 1查看 257关注 0票数 0

我正在使用Selenide和Phantomjs来测试test应用程序。我将截图如下:

代码语言:javascript
复制
byte[] bytes = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.BYTES);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
returnImage = ImageIO.read(bis);

但是,对于输入元素,屏幕截图如下所示:faulty screenshot

它实际上看起来像这样(当使用chrome/firefox时) how it's supposed to look

有趣的是,当我将Selenide设置为使用phantomjs (Configuration.Browser = "phantomjs")时,它会正确地截取屏幕截图。它也只出现在那种元素上。按钮等可以正常录制。有什么想法吗?

附于这篇文章的截图是被裁剪的,这里的代码截取了整个页面的截图。在我的代码中,我只将屏幕截图裁剪到所需的元素,但即使在显示整个屏幕的屏幕截图上,元素也没有被正确记录。

EN

回答 1

Stack Overflow用户

发布于 2017-09-08 17:07:38

你可以用下面的代码截取一个元素的屏幕:

代码语言:javascript
复制
WebElement ele = driver.findElement(By.id("hplogo"));

// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);

// Get the location of element on the page
Point point = ele.getLocation();

// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
    eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);

// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);

现在,在上面的代码中,您将获得getWidth()和getHeight()。您可以尝试通过添加或减去该值来调整它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46112208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档