简体   繁体   中英

How to take screenshot with url in selenium c#

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM