Home > Software engineering >  How to take screenshot with url in selenium c#
How to take screenshot with url in selenium c#

Time:12-10

How can I add the url to the screenshot with c# in Selenium. Is there a good solution for this?

CodePudding user response:

I tried the solution. it works and get the url displayed.


Use the steps below to add a URL to a screenshot in Selenium with C#

1- Create a extension:

     public static Screenshot AddURLToImage(this Screenshot screenshot, string url)
        {
            string base64 = String.Empty;
            using (var ms = new MemoryStream(screenshot.AsByteArray, 0, screenshot.AsByteArray.Length))
            {
                Image image = Image.FromStream(ms, true);
                Graphics graphics = Graphics.FromImage(image);
                using (var font = new Font("Arial", 11))
                {
                    graphics.DrawString(url, font, Brushes.White, 10, 10);
                    var imageConverter = new ImageConverter();
                    byte[] buffer = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
                    base64 = Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);
                }
            }
    
            var screenshotWithUrl = new Screenshot(base64);
            return screenshotWithUrl;
        }

2- Get a screenshot with Selenium

   Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();

3- Add URL to image and save it

    screenshot.AddURLToImage(url).SaveAsFile(path);
  • Related