• 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);
                            }
    
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    MySQL集群在断网后再启动报"Unable to start missing node group"问题处理
    由于OCR文件损坏造成Oracle RAC不能启动的现象和处理方法
    使用dbms_scheduler包创建定时任务
    Oracle_12c_RAC_service_died问题分析处理
    MySQL使用正则表达式比较字段中的数字
    crontab不能正确执行的问题
    Oracle 12c SYSAUX表空间不足处理-清理audsys.cli_swp$a9b5f52c$1$1表
    HyperV上的Linux虚拟机启动报panic_early_exception错误
    Nagios NSclient Failed to get CPU value: 238(_total)6: Failed to get mutex :(
    两数相加II--链表
  • 原文地址:https://www.cnblogs.com/tangchun/p/10614974.html
Copyright © 2020-2023  润新知