Jessica David
Jessica David

Reputation: 127

Screenshot of a full element in Selenium C#

I need to take a screenshot of a whole element in selenium C# using chromedriver. The element is table and though I am getting the width and height of the element, the screenshot I am getting is of only 15 rows.

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

Is there any way other than this from which I can get the screenshot of the whole table scrolling the webpage?

Upvotes: 0

Views: 3224

Answers (2)

Noksa
Noksa

Reputation: 71

You can use this package https://www.nuget.org/packages/Noksa.WebDriver.ScreenshotsExtensions/

In order to take a screenshot of the element, use the OnlyElementDecorator:

using WDSE;
using WDSE.Decorators;
using WDSE.ScreenshotMaker;

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

Upvotes: 1

8WmK
8WmK

Reputation: 106

Does the table have an ID? If so you could take a screenshot of the table using that id rather than the element name?

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

From the similar question (but not Xpath): How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

Seems to suggest you might need to force the window to be larger before taking the screenshot?

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

Upvotes: 1

Related Questions