Screenshot of a full element in Selenium C#
我需要使用chromedriver对Selenium C#中的整个元素进行截图。 元素是表格,尽管我正在获取元素的宽度和高度,但我得到的屏幕截图只有15行。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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)); |
除此之外,还有其他方法可以使我获得整个表格在滚动网页时的屏幕截图吗?
您可以使用此程序包https://www.nuget.org/packages/Noksa.WebDriver.ScreenshotsExtensions/
为了获取元素的屏幕快照,请使用OnlyElementDecorator:
1 2 |
该表是否有ID? 如果是这样,您可以使用该ID而不是元素名称来获取表的屏幕快照?
1 | WebElement ele = driver.findElement(By.id("yourTableID")); |
从类似的问题(但不是Xpath):
如何使用Selenium Webdriver捕获特定元素而不是整个页面的屏幕截图?
似乎建议您在截屏之前可能需要强制将窗口变大?
1 |