简体   繁体   English

如何在 selenium c# 中使用 url 截屏

[英]How to take screenshot with url in selenium c#

How can I add the url to the screenshot with c# in Selenium.如何使用 Selenium 中的 c# 将 url 添加到屏幕截图中。 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#它可以工作并显示 url 使用以下步骤将 URL 添加到 ZC49DFB55F06BB406E2C5CA786FZDB7D38239D7EFA19FBE24D 中的屏幕截图中

1- Create a extension: 1-创建一个扩展:

     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 2- 使用 Selenium 获取屏幕截图

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

3- Add URL to image and save it 3- 将 URL 添加到图像并保存

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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