在这里,我编写了一些代码以使用C#拍摄Element的屏幕截图
FirefoxDriver driver = null; private WebDriverWait wait; // Use this function to take screenshot of an element.public static Bitmap GetElementScreenShot(IWebDriver driver, IWebElement element){ Screenshot sc = ((ITakesScreenshot)driver).GetScreenshot(); var img = Image.FromStream(new MemoryStream(sc.AsByteArray)) as Bitmap; return img.Clone(new Rectangle(element.Location, element.Size), img.PixelFormat);} //testing function public void GetIPLocation(string IPAddress) { try { if (driver == null) driver = new FirefoxDriver(); if (driver.Title != "IP Location Finder - Geolocation") driver.Navigate().GoToUrl("https://www.iplocation.net/"); if (wait == null) wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60)); var ipTextBox = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("input[type='text']"))); ipTextBox.Clear(); ipTextBox.SendKeys(IPAddress); wait.Until(ExpectedConditions.ElementExists(By.CssSelector("input[type='submit']"))).Click(); foreach (IWebElement element in driver.FindElements(By.CssSelector("div>.col.col_12_of_12"))) { if (element.FindElements(By.TagName("h4")).Count > 0) { var img = GetElementScreenShot(driver, element); img.Save("test.png", System.Drawing.Imaging.ImageFormat.Png); } } } catch (Exception) { throw; } }如果有任何问题,请通知我。



