Selenium C#

时间:2018-04-24 15:51:09

标签: c# selenium selenium-webdriver

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

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

除此之外还有什么其他方法可以从中获取滚动网页的整个表格的屏幕截图吗?

2 个答案:

答案 0 :(得分:0)

表格有ID吗?如果是这样,您可以使用该ID而不是元素名称来截取表格的截图吗?

WebElement ele = driver.findElement(By.id("yourTableID"));

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

似乎建议您在拍摄屏幕截图之前可能需要强制窗口更大?

this.driver.manage().window().setSize(new Dimension(1680, 1050));

答案 1 :(得分:0)

您可以使用此软件包https://www.nuget.org/packages/Noksa.WebDriver.ScreenshotsExtensions/

为了获取元素的屏幕快照,请使用OnlyElementDecorator:

var vcs = new OnlyElementDecorator(new ScreenshotMaker());
var screen = _driver.TakeScreenshot(vcs);