首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium C#中一个完整元素的屏幕截图

Selenium C#中一个完整元素的屏幕截图
EN

Stack Overflow用户
提问于 2018-04-24 15:51:08
回答 2查看 2.4K关注 0票数 0

我需要使用chromedriver来截图selenium C#中的整个元素。元素是表,虽然我得到元素的宽度和高度,但我得到的屏幕截图只有15行。

代码语言:javascript
复制
IWebElement element = driver.FindElement(By.XPath("Xpath of the element"));

string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".jpg";

Byte[] byteArray = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;

System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));

System.Drawing.Rectangle croppedImage = new System.Drawing.Rectangle(element.Location.X, element.Location.Y,   element.Size.Width, element.Size.Height);

screenshot = screenshot.Clone(croppedImage, screenshot.PixelFormat);

screenshot.Save(String.Format(@"path" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg));

除了这个之外,还有其他方法可以让我看到整个表格的屏幕截图吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-24 15:56:13

这张桌子有身份证吗?如果是这样的话,您可以使用该id而不是元素名来截取表的屏幕快照吗?

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

来自类似的问题(但不是Xpath):How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

似乎意味着你可能需要在拍摄截图之前强迫窗口变大?

代码语言:javascript
复制
this.driver.manage().window().setSize(new Dimension(1680, 1050));
票数 1
EN

Stack Overflow用户

发布于 2018-10-22 07:39:56

您可以使用这个包https://www.nuget.org/packages/Noksa.WebDriver.ScreenshotsExtensions/

为了获取元素的屏幕截图,请使用OnlyElementDecorator:

代码语言:javascript
复制
using WDSE;
using WDSE.Decorators;
using WDSE.ScreenshotMaker;

var vcs = new OnlyElementDecorator(new ScreenshotMaker());
var screen = _driver.TakeScreenshot(vcs);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50005998

复制
相关文章

相似问题

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