zoukankan      html  css  js  c++  java
  • ChromeDriver截图

    一、NuGet安装Selenium.Chrome.WebDriver和Selenium.WebDriver

    二、将packagesSelenium.Chrome.WebDriver.2.45driver下的chromedriver.exe文件,复制到程序的bin目录下

    三、截图工具类

    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.UI;
    using System;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.IO;
    using System.Threading;
    
    namespace Comon
    {
        public class ScreenshotUtil : IDisposable
        {
            /// <summary>
            ////// </summary>
            private static readonly object LockObject = new object();
    
            private static IWebDriver _webDriver;
    
            public ScreenshotUtil(string url, int windowWidth, int windowHeight)
            {
                if (_webDriver == null)
                {
                    lock (LockObject)
                    {
                        if (_webDriver == null)
                        {
                            ChromeOptions options = new ChromeOptions();
                            options.AddArgument($"--window-size={windowWidth},{windowHeight}");
                            options.AddArgument("start-fullscreen");
                            _webDriver = new ChromeDriver(options);
                        }
                    }
                }
                if (_webDriver.Url != url)
                {
                    _webDriver.Navigate().GoToUrl(url);
                }
            }
    
            public void Dispose()
            {
                _webDriver?.Quit();
                _webDriver = null;
            }
    
            public static void Quit()
            {
                lock (LockObject)
                {
                    _webDriver?.Quit();
                    _webDriver = null;
                }
            }
    
            public void ShotThenSave(string targetFileName, TimeSpan waitTime, Func<IWebDriver, IWebElement> condition)
            {
                if (!File.Exists(targetFileName))
                {
                    lock (LockObject)
                    {
                        if (string.IsNullOrWhiteSpace(targetFileName))
                        {
                            throw new ArgumentNullException(nameof(targetFileName));
                        }
                        if (condition != null)
                        {
                            WebDriverWait webDriverWait = new WebDriverWait(_webDriver, waitTime);
                            webDriverWait.Until(condition);
                        }
    
                        Thread.Sleep(2000);
    
                        ITakesScreenshot screenshotDriver = (ITakesScreenshot)_webDriver;
                        if (screenshotDriver != null)
                        {
                            Screenshot screenshot = screenshotDriver.GetScreenshot();
                            string directoryName = Path.GetDirectoryName(targetFileName);
                            if (directoryName == null)
                            {
                                throw new ArgumentException(nameof(targetFileName));
                            }
                            if (!Directory.Exists(directoryName))
                            {
                                Directory.CreateDirectory(directoryName);
                            }
                            ClearOldImage(directoryName);
    
                            screenshot.SaveAsFile(targetFileName);
                        }
                    }
                }
            }
    
            private static void ClearOldImage(string directoryName)
            {
                DirectoryInfo folder = new DirectoryInfo(directoryName);
                FileInfo[] existFileInfos = folder.GetFiles();
                if (existFileInfos.Length > 1500)
                {
                    foreach (var file in existFileInfos)
                    {
                        if (file.CreationTime < DateTime.Today)
                        {
                            try
                            {
                                file.Delete();
                            }
                            catch (Exception e)
                            {
                                LogUtil.LogException(e);
                            }
    
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    用opengl实现多个视口
    齐次坐标和矩阵变换
    关于透明和不透明排序问题
    PlaneBoundedVolumeList平面体积查询
    jQuery获取元素
    关于借助prototype进行分页的一个小插件
    浏览器解析状态
    关于获取元素进行动画效果的问题以及简单的正则表达式验证
    php简单分页类
    生产者消费者问题
  • 原文地址:https://www.cnblogs.com/tangchun/p/10614974.html
Copyright © 2011-2022 走看看