public static ChromeDriver driver; private readonly string driverLocation = Environment.GetEnvironmentVariable("CHROMEWEBDRIVER"); private bool Chrome_Login(bool BrowserView = true) { try { string Path = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\'); new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.Latest); var driverService = ChromeDriverService.CreateDefaultService(); ChromeOptions options = new ChromeOptions(); if (BrowserView == false) { options.AddArgument("--headless");//完全非表示 } options.AddArgument("no-sandbox");//240517追記 options.AddExcludedArgument("enable-automation"); options.AddUserProfilePreference("download.default_directory", Path); driverService.HideCommandPromptWindow = true;//コマンドウィンドウ非表示 driver = new ChromeDriver(driverService, options, TimeSpan.FromSeconds(20));// driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);// } catch (OpenQA.Selenium.WebDriverException) { MessageBox.Show("Chromeを起動してバージョンを最新にしてください"); Process[] processes = Process.GetProcessesByName("chromedriver"); foreach (Process process in processes) { process.Kill(); } } } private void TEST(int NowRow = 0) { bool KidouCheck = false; if (driver == null)//nullになっているパターンと、なっていないパターンがある { KidouCheck = Chrome_Login(); if (KidouCheck == false) return; } driver.Url = "http:";//←手動でChromeを閉じると、すでに閉じたとエラーになる }
if (driver == null) { KidouCheck = Chrome_Login(); if (KidouCheck == false) return; } try { var ChromeHandle=chrome.CurrentWindowHandle; } catch (NoSuchWindowException ex) { KidouCheck = Chrome_Login(); }