0

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?

Jessica David
  • 117
  • 3
  • 11

2 Answers2

1

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));
8WmK
  • 106
  • 1
  • 8
  • No. The table doesn't have Id. That is why, I am using Xpath – Jessica David Apr 24 '18 at 15:59
  • From the similar question (but not Xpath): https://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-a-specific-element-rather-than-entire-page-usin 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)); – 8WmK Apr 24 '18 at 16:10
1

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);
Community
  • 1
  • 1
Noksa
  • 71
  • 1
  • 1