c#初始化ChromeDriver驅動,隱藏服務黑窗口,禁止pdf直接瀏覽,指定下載目錄,是否開啓無痕模式等等..

c#初始化ChromeDriver驅動,隱藏服務黑窗口,禁止pdf直接瀏覽,指定下載目錄,是否開啓無痕模式等等..

    public class RunChrome
    {
        /// <summary>
        /// Chrome 驅動服務對象
        /// </summary>
        public static ChromeDriverService CDS = null;

        /// <summary>
        /// 初始化ChromeDriver對象
        /// </summary>
        /// <param name="IsHeadLess">是否開啓無頭瀏覽器(隱藏瀏覽器)</param>
        /// <param name="IsJZimg">是否禁止加載圖片文件</param>
        /// <returns></returns>
        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<string, object>
            //{
            //    ["enabled"] = true,
            //    ["name"] = "Chrome PDF Viewer"
            //};
            //var pluginsList = new Dictionary<string, object>
            //{
            //    { "plugins_list", new [] { pdfViewerPlugin } }
            //};
            //var downloadPreferences = new Dictionary<string, object>
            //{
            //    {"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);
   
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章