以下以edge浏览器为例,自动判断selenium驱动版本号和当前用户使用的浏览器版本号是否一致,若不一致则自动下载对应版本号的驱动,使之保持对应关系,这样就不需要每次手动对程序进行升级去适配对应版本的驱动程序了,代码仅供参考,其他内核如chrome,firefox等等,原理类似,希望对大家有所帮助!
//获取EdgeDriver浏览器版本 string driverVersion = string.Empty; if (File.Exists(@".\msedgedriver.exe")) { driverVersion = FileVersionInfo.GetVersionInfo(@".\msedgedriver.exe").FileVersion; } //获取Edge浏览器路径 string[] edgeVersion = new string[2]; string path = string.Empty; string regPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe"; Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath); if (key != null) { path = key.GetValue("").ToString(); } //获取Edge浏览器版本 edgeVersion[0] = FileVersionInfo.GetVersionInfo(path).FileVersion; if (driverVersion != edgeVersion[0]) { //获取Edge浏览器位数 if (IntPtr.Size == 8) { edgeVersion[1] = "32"; } else { edgeVersion[1] = "64"; } //获取EdgeDriver下载地址 string downloadUrl = "https://msedgedriver.azureedge.net/" + edgeVersion[0] + "/edgedriver_win" + edgeVersion[1] + ".zip"; //下载EdgeDriver using (WebClient client = new WebClient()) { client.DownloadFile(downloadUrl, @".\msedgedriver.zip"); } //解压EdgeDriver using (ZipArchive archive = ZipFile.OpenRead(@".\msedgedriver.zip")) { foreach (ZipArchiveEntry entry in archive.Entries) { string releasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, entry.FullName); if (File.Exists(releasePath)) { File.Delete(releasePath); } entry.ExtractToFile(releasePath); } } }
评论前必须登录!
注册