0
我一直在尋找一些如何在C#中的Selenium Webdriver中使用ITakesScreenshot截圖的好例子,然後使用元素尺寸裁剪圖像,然後保存這個新圖像。沒有這樣的幸運,我在C#中找到了任何東西。有沒有什麼好的例子,如何在selenium webdriver C#中截圖,然後裁剪並保存圖像?
我現在有這個方法,但每隔一段時間我在測試中使用時都會遇到Out of Memory異常。它在嘗試裁剪的行上失敗。
public void TakeScreenShotOfElement(IWebDriver _driver, string rootpath, string imgName, string element2)
{
string element3 = element2;
var element = driver.FindElement(By.XPath(element3));
Byte[] ba = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
var ss = new Bitmap(new MemoryStream(ba));
var crop = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
//create a new image by cropping the original screenshot
Bitmap image2 = ss.Clone(crop, ss.PixelFormat);
if (!Directory.Exists(rootpath))
{
Directory.CreateDirectory(rootpath);
}
image2.Save(String.Format("{0}\\{1}.png", rootpath, imgName), System.Drawing.Imaging.ImageFormat.Png);
}
任何幫助非常感謝,提前致謝!
謝謝,這很好。 – user3782210 2014-09-05 08:43:23