Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Selenium Chrome error: cannot convert from 'System.Drawing.Imaging.ImageFormat' to 'OpenQA.Selenium.ScreenshotImageFormat'

I have a problem with selenium in C#. Below is my code but I have still the same error: cannot convert from 'System.Drawing.Imaging.ImageFormat' to 'OpenQA.Selenium.ScreenshotImageFormat'

How can I fix it? Any ideas?

using System;
using System.Drawing.Imaging;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Drawing;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("xx://xx.xx.xx");
            driver.FindElement(By.Id("xx")).SendKeys("xxx");
            driver.FindElement(By.Id("xx")).SendKeys("xxx");
            driver.FindElement(By.Id("xx")).Click();

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile(@"C:\\test.Png", System.Drawing.Imaging.ImageFormat.Png);

            Console.Read();
            Console.WriteLine("xxx");
        }

    }
}
like image 280
Freeezer789 Avatar asked Sep 20 '17 14:09

Freeezer789


2 Answers

Instead of System.Drawing.Imaging.ImageFormat.Png as the 2nd param of your SaveAsFile, you need to use OpenQA.Selenium.ScreenshotImageFormat.[something here]

That enum probably has it's own PNG format.

like image 52
Neil N Avatar answered Nov 05 '22 11:11

Neil N


The error was persistent, saying:

System.Drawing.Imaging.ImageFormat is not supported. Please change format.

Then I changed code from:

ScreenShot.SaveAsFile(FileName.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg); 

To:

ScreenShot.SaveAsFile(FileName.ToString(), ScreenshotImageFormat.Jpeg);

And it worked.

like image 45
Tester Avatar answered Nov 05 '22 10:11

Tester