1. <legend id='pzA5S'><style id='pzA5S'><dir id='pzA5S'><q id='pzA5S'></q></dir></style></legend>

        <bdo id='pzA5S'></bdo><ul id='pzA5S'></ul>

      <small id='pzA5S'></small><noframes id='pzA5S'>

      <tfoot id='pzA5S'></tfoot>
    1. <i id='pzA5S'><tr id='pzA5S'><dt id='pzA5S'><q id='pzA5S'><span id='pzA5S'><b id='pzA5S'><form id='pzA5S'><ins id='pzA5S'></ins><ul id='pzA5S'></ul><sub id='pzA5S'></sub></form><legend id='pzA5S'></legend><bdo id='pzA5S'><pre id='pzA5S'><center id='pzA5S'></center></pre></bdo></b><th id='pzA5S'></th></span></q></dt></tr></i><div id='pzA5S'><tfoot id='pzA5S'></tfoot><dl id='pzA5S'><fieldset id='pzA5S'></fieldset></dl></div>

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

      How can I get screenshot of specified element using WebDriver in C#(如何在 C# 中使用 WebDriver 获取指定元素的屏幕截图)
        <bdo id='TdM4h'></bdo><ul id='TdM4h'></ul>

                <legend id='TdM4h'><style id='TdM4h'><dir id='TdM4h'><q id='TdM4h'></q></dir></style></legend>
              1. <tfoot id='TdM4h'></tfoot>

                <small id='TdM4h'></small><noframes id='TdM4h'>

              2. <i id='TdM4h'><tr id='TdM4h'><dt id='TdM4h'><q id='TdM4h'><span id='TdM4h'><b id='TdM4h'><form id='TdM4h'><ins id='TdM4h'></ins><ul id='TdM4h'></ul><sub id='TdM4h'></sub></form><legend id='TdM4h'></legend><bdo id='TdM4h'><pre id='TdM4h'><center id='TdM4h'></center></pre></bdo></b><th id='TdM4h'></th></span></q></dt></tr></i><div id='TdM4h'><tfoot id='TdM4h'></tfoot><dl id='TdM4h'><fieldset id='TdM4h'></fieldset></dl></div>

                  <tbody id='TdM4h'></tbody>
                本文介绍了如何在 C# 中使用 WebDriver 获取指定元素的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的小项目是用 Java 编写的,我需要用 C# 重写它.

                I have my little project written on Java and I need to rewrite it in C#.

                几乎完成了,但我一直坚持使用 Selenium webdriver 获取元素的屏幕截图.我用 Java 做的下一个方法:

                It's almost done, but I am stuck on getting screenshot of element using Selenium webdriver. I did it in Java in the next way:

                    public String saveImage(){
                        String src = "";
                        try{
                            File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                            BufferedImage fullImg = ImageIO.read(screenshot);
                            Point point = elementToScreent.getLocation();
                            int eleWidth = elementToScreent.getSize().getWidth();
                            int eleHeight = elementToScreent.getSize().getHeight();
                            BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
                            eleHeight);
                            ImageIO.write(eleScreenshot, "png", screenshot);
                            src = path + System.currentTimeMillis() +".png";
                            FileUtils.copyFile(screenshot, new File(src));
                    }catch(Exception e){
                        e.printstacktrace();
                    }
                    return src;
                }
                

                它在 Java 中完美运行,但我不知道如何用 C# 重写它,因为我不太熟悉它.

                It works perfect in Java, but I have no idea how to rewrite it in C#, as I am not so familiar with it.

                有人可以提出一些在 C# 中实现相同目标的好方法吗?

                Could someone suggest some nice way to achieve the same in C#?

                推荐答案

                这里我写了一些代码来用c#截取一个元素

                Here i have written some code to take screenshot of an Element using c#

                 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;
                        }
                    }
                

                如果有任何问题,请告诉我.

                if any issue then let me know.

                这篇关于如何在 C# 中使用 WebDriver 获取指定元素的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <i id='txjuR'><tr id='txjuR'><dt id='txjuR'><q id='txjuR'><span id='txjuR'><b id='txjuR'><form id='txjuR'><ins id='txjuR'></ins><ul id='txjuR'></ul><sub id='txjuR'></sub></form><legend id='txjuR'></legend><bdo id='txjuR'><pre id='txjuR'><center id='txjuR'></center></pre></bdo></b><th id='txjuR'></th></span></q></dt></tr></i><div id='txjuR'><tfoot id='txjuR'></tfoot><dl id='txjuR'><fieldset id='txjuR'></fieldset></dl></div>
                  • <legend id='txjuR'><style id='txjuR'><dir id='txjuR'><q id='txjuR'></q></dir></style></legend>
                      <tfoot id='txjuR'></tfoot>

                        <bdo id='txjuR'></bdo><ul id='txjuR'></ul>
                          <tbody id='txjuR'></tbody>
                      • <small id='txjuR'></small><noframes id='txjuR'>