private static ChromeVersion GetLocalChromeDriverVersion()
        {
            ChromeVersion retVal           = null;
            string        chromeDriverPath = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                chromeDriverPath = Path.Combine(Environment.CurrentDirectory, ".\\chromedriver.exe");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                chromeDriverPath = Path.Combine(Environment.CurrentDirectory, "chromedriver");
            }


            if (File.Exists(chromeDriverPath))
            {
                Process p = new Process();

                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName  = chromeDriverPath;
                p.StartInfo.Arguments = "--version";
                p.Start();

                string[] output = p.StandardOutput.ReadToEnd().Split(" ");
                p.WaitForExit();

                retVal = new ChromeVersion(output[1]);
            }

            return(retVal);
        }
        public static void DownloadChromeDriver()
        {
            ChromeVersion localChromeVersion = ChromeVersions.LocalChrome;

            if (localChromeVersion == null)
            {
                throw new FileNotFoundException("No chrome installed. Please install chrome first.");
            }

            ChromeVersion localChromeDriverVersion = ChromeVersions.LocalChromeDriver;

            if (localChromeDriverVersion != null)
            {
                Console.WriteLine("chromedriver.exe already available in folder {0}. Checking version...", Environment.CurrentDirectory);

                if (localChromeVersion.EqualsExcludingPatchVersion(localChromeDriverVersion))
                {
                    Console.WriteLine("Version {0} of local chromedriver.exe fits to version {1} of chrome", localChromeDriverVersion, localChromeVersion);
                    return;
                }
                else
                {
                    Console.WriteLine("Version {0} of local chromedriver.exe does not fit to version {1} of chrome", localChromeDriverVersion, localChromeVersion);
                }
            }
            else
            {
                Console.WriteLine("No local version of chromedriver.exe found in folder {0}", Environment.CurrentDirectory);
            }

            DownloadChromeDriver(localChromeVersion);
        }
        private static ChromeVersion GetLocalChromeVersion()
        {
            ChromeVersion retVal = null;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                string chromePathProgramFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) +
                                                   "\\Google\\Chrome\\Application\\chrome.exe";
                string chromePathProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
                                                "\\Google\\Chrome\\Application\\chrome.exe";

                string chromePath = File.Exists(chromePathProgramFiles) ? chromePathProgramFiles : chromePathProgramFilesX86;
                if (File.Exists(chromePath))
                {
                    retVal = new ChromeVersion(FileVersionInfo.GetVersionInfo(chromePath).FileVersion);
                }
                // Tested on mac with intel silicon and macos Big Sur 11.2.3
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                string appsPath        = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                string chromePlistPath = Path.GetFullPath(Path.Combine(appsPath, "Google Chrome.app", "Contents", "Info.plist"));

                XDocument chromePlist     = XDocument.Load(chromePlistPath);
                var       elements        = chromePlist.Elements("plist").DescendantNodesAndSelf().ToList();
                bool      versionKeyFound = false;
                string    version         = string.Empty;

                foreach (var element in elements)
                {
                    if (element.ToString() == "KSVersion")
                    {
                        versionKeyFound = true;
                        continue;
                    }

                    if (versionKeyFound)
                    {
                        version = element.ToString();
                        version = version.Replace("<string>", "");
                        version = version.Replace("</string>", "");
                        break;
                    }
                }

                retVal = new ChromeVersion(version);
            }

            return(retVal);
        }
        internal static ChromeVersion GetChromeDriverVersionByChromeVersion(ChromeVersion chromeVersion)
        {
            string chromeDriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" + chromeVersion.GetVersionExcludingPatchVersion();

            Console.WriteLine("Getting latest version of chromdriver.exe for chrome version {0} from {1}", chromeVersion, chromeDriverVersionUrl);

            HttpResponseMessage response = httpClient.GetAsync(chromeDriverVersionUrl).Result;

            string chromeDriverVersion = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine("Found chromdriver version {0} for chrome version {1}", chromeDriverVersion, chromeVersion);

            return(new ChromeVersion(chromeDriverVersion));
        }
        private static void DownloadChromeDriver(ChromeVersion chromeVersion)
        {
            ChromeVersion chromeDriverVersion = ChromeVersions.GetChromeDriverVersionByChromeVersion(chromeVersion);

            string chromeDriverUrl         = "";
            string chromeDriverZipFilePath = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                chromeDriverUrl         = "https://chromedriver.storage.googleapis.com/" + chromeDriverVersion + "/chromedriver_win32.zip";
                chromeDriverZipFilePath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "chromedriver_win32.zip");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                string tempPath = Path.GetTempPath();

                chromeDriverUrl         = "https://chromedriver.storage.googleapis.com/" + chromeDriverVersion + "/chromedriver_mac64.zip";
                chromeDriverZipFilePath = Path.GetFullPath(Path.Combine(tempPath, "chromedriver_mac64.zip"));
            }

            Console.WriteLine("Downloading chromedriver version {0} from {1} to local folder {2}", chromeDriverVersion, chromeDriverUrl, chromeDriverZipFilePath);

            using (HttpResponseMessage response = httpClient.GetAsync(chromeDriverUrl).Result)
            {
                response.EnsureSuccessStatusCode();

                using (Stream contentStream = response.Content.ReadAsStreamAsync().Result)
                {
                    string targetFile = chromeDriverZipFilePath;

                    using (FileStream fileStream = File.Create(targetFile))
                    {
                        contentStream.Seek(0, SeekOrigin.Begin);
                        contentStream.CopyTo(fileStream);
                    }
                }
            }

            Console.WriteLine("Extracting {0} to {1}", chromeDriverZipFilePath, Environment.CurrentDirectory);

            ZipFile.ExtractToDirectory(chromeDriverZipFilePath, ".", true);
            File.Delete(chromeDriverZipFilePath);
        }