ChromeDriver截图

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

二、将packages\Selenium.Chrome.WebDriver.2.45\driver下的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);
                        }

                    }
                }
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/tangchun/p/10614974.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值