栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在C#中使用WebDriver获取指定元素的屏幕截图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在C#中使用WebDriver获取指定元素的屏幕截图

在这里,我编写了一些代码以使用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;        }    }

如果有任何问题,请通知我。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452324.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号