c#初始化ChromeDriver驱动,隐藏服务黑窗口,禁止pdf直接浏览,指定下载目录,是否开启无痕模式等等..

c#初始化ChromeDriver驱动,隐藏服务黑窗口,禁止pdf直接浏览,指定下载目录,是否开启无痕模式等等..

    public class RunChrome
    {
        /// 
        /// Chrome 驱动服务对象
        /// 
        public static ChromeDriverService CDS = null;

        /// 
        /// 初始化ChromeDriver对象
        /// 
        /// 是否开启无头浏览器(隐藏浏览器)
        /// 是否禁止加载图片文件
        /// 
        public static ChromeDriver getChromeDriver(bool IsHeadLess = false, bool IsJZimg = true)
        {
            ChromeOptions options = new ChromeOptions();
            
            //指定启动的chrome.exe;
            options.BinaryLocation = EsToCgrs.MyLogHelper.CurrentPath + "chrome\\chrome.exe";

            options.AddArgument("lang=zh_CN.UTF-8");
            
            options.AddArguments("--no-sandbox", "--test-type"); // "--incognito"
            
            //开启无痕浏览器 "--incognito"  开启无痕浏览器
            options.AddArguments("--incognito"); // "--incognito"
           
            options.AddArguments("--disable-infobars", "--disable-gpu", "--enable-strict-powerful-feature-restrictions");

            options.AddArguments("--disable-plugins", "--disable-images", "--start-maximized");

            options.AddUserProfilePreference("profile.default_content_settings", 2);
            options.AddUserProfilePreference("profile.default_content_setting_values", 2);

            //设置默认下载路径
            options.AddUserProfilePreference("download.default_directory", @"D:\ChromeDownFile\");
            //options.AddUserProfilePreference("download.prompt_for_download", false);
            options.AddUserProfilePreference("disable-popup-blocking", "true");
            options.AddUserProfilePreference("intl.accept_languages", "nl");
            

            //禁用pdf直接浏览
            options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

            //禁用pdf直接浏览
            //options.AddUserProfilePreference("plugins.plugins_disabled", new[] { "Chrome PDF Viewer" });

            //var pdfViewerPlugin = new Dictionary
            //{
            //    ["enabled"] = true,
            //    ["name"] = "Chrome PDF Viewer"
            //};
            //var pluginsList = new Dictionary
            //{
            //    { "plugins_list", new [] { pdfViewerPlugin } }
            //};
            //var downloadPreferences = new Dictionary
            //{
            //    {"default_directory", @"D:\pande\"},
            //    {"directory_upgrade", true}
            //};
            //options.AddUserProfilePreference("download", downloadPreferences);
            //options.AddUserProfilePreference("plugins", pluginsList);

            //禁止弹出通知消息
            options.AddUserProfilePreference("profile.default_content_setting_values.notifications", 2);

            if (IsHeadLess)
            {
                //开启无头模式 - 隐藏浏览器 无界面
                options.AddArgument("headless");
            }

            if (IsJZimg)
            {
                //禁止加载图片
                options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
            }

            CDS = ChromeDriverService.CreateDefaultService();

            //是否应隐藏服务的命令提示符窗口
            CDS.HideCommandPromptWindow = true;

            CDS.Start();

            var chromedriver = new ChromeDriver(CDS, options);

            //页面加载超时时间
            chromedriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(60);

            //设置隐式等待超时随机等待10-20秒
            chromedriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(new Random().Next(10, 15));

            return chromedriver;
        }

    }

如果页面超时加载报错,可通过禁止加载让程序继续执行,看下代码:

try
{
	Pdriver.Navigate().GoToUrl(url);
}
catch(Exception ex)
{
	Pdriver.ExecuteScript("window.stop ? window.stop() : document.execCommand(\"Stop\");");
	logQueue.Enqueue("错误:\r\n" + ex.Message);
   
}

 

你可能感兴趣的:(c#,ChromeDriver,chrome,ExecuteScript,ChromeDriver,禁止pdf)