How can I get screenshot of specified element using WebDriver in C#

旧城冷巷雨未停 提交于 2019-12-18 05:24:09

问题


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

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

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

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


回答1:


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

 FirefoxDriver driver = null;
    private WebDriverWait wait;

    // Use this function to take screen short of an element.  

public static Bitmap GetElementScreenShort(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 = GetElementScreenShort(driver, element);
                    img.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

if any issue then let me know.




回答2:


I didn't check the code, but must work perfectly:

public Bitmap MakeElemScreenshot( IWebDriver driver, WebElement elem)
{
    Screenshot myScreenShot = ((ITakesScreenshot)driver).GetScreenshot();

    Bitmap screen = new Bitmap(new MemoryStream(myScreenShot.AsByteArray));
    Bitmap elemScreenshot = screen.Clone(new Rectangle(elem.Location, elem.Size), screen.PixelFormat);

    screen.Dispose();

    return elemScreenshot;
}

By the way, it's c# code. But java's code will be similar to this.



来源:https://stackoverflow.com/questions/35921168/how-can-i-get-screenshot-of-specified-element-using-webdriver-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!