Objective
The objective of this post is to auto find the need for new driver download with the real time analysis of errors during web page launching. All examples om this post are based on C# and we will look into Chrome and Edge browsers. auto download feature is already part of Selenium Grid now a days but we can extend it to local setup as well
Case 1: Edge
This is a chromium based browser created by Microsoft. below sample is launch of webpage code:
DriverPath = "<path of the msedgedriver.exe>";
var optiones = new EdgeOptions();
optiones.AddArgument("--no-sandbox");
IWeb = new EdgeDriver(DriverPath,optiones);
In case of wrong msedgedriver.exe which is not matching the actual browsers we will get the error message like: This version of MSEdgeDriver only supports MSEdge version 84 (SessionNotCreated)
We have to check if the error string contains “only supports MSEdge version “
If the above test we can follow below steps to download the correct driver versions.
Step 1: Check existing browser version on the machine
var path = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe", "", null);
string version = FileVersionInfo.GetVersionInfo(path.ToString()).FileVersion;
Step 2: Use the version from above code to run an API
Method: GET
URL: https://msedgedriver.azureedge.net/version/edgedriver_win32.zip
Content-Type: application/x-www-form-urlencoded
Keep Alive: True
This will download the right version of msedgedriver.exe
Case 2: Chrome
This is a chromium based browser created by Google. below sample is launch of webpage code:
DriverPath = "<path of the chromedriver.exe>";
var optiones = new ChromeOptions();
optiones.AddArgument("--no-sandbox");
IWeb = new ChromeDriver(DriverPath,optiones);
We will get similar error as in the case of mismatched edge driver for chrome as well. In case the error is encountered use below code to fetch the current chrome version
Step 1: Get Correct Chrome version Installed
var path = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null);
string version = FileVersionInfo.GetVersionInfo(path.ToString()).FileVersion;
Step 2: Based on the version from Step 1 get mapped driver version
Use below code to get version mappings
//URL's originates from here: https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.json
string html = string.Empty;
string urlToPathLocation = @"https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.json";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToPathLocation);
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
string VerJSON = html;
JObject jobj = JObject.Parse(VerJSON);
string vmaJOR = version.Split('.')[0];
dynamic JT = jobj["milestones"][vmaJOR]["downloads"]["chromedriver"];
string vversion = version;
for(int y=0;y<JT.Count;y++)
{
if((JT[y]["platform"].Value).ToString()=="win32")
{
vversion = ((JT[y]["url"].Value).ToString().Replace("https://storage.googleapis.com/chrome-for-testing-public/", "").Replace("/win32/chromedriver-win32.zip", ""));
}
}
Step 3: Download Verion of chromedriver.exe
Method: GET
URL: https://storage.googleapis.com/chrome-for-testing-public//vversion /win32/chromedriver-win32.zip
Content-Type: application/x-www-form-urlencoded
Keep Alive: True
This will download the right version of chromedriver.zip
fpath = “path of the zip file”;
try
{
System.IO.Compression.ZipFile.ExtractToDirectory(fpath + “//” + “chromedriver.zip”, fpath);
if(File.Exists(fpath + “//” + “chromedriver-win32/chromedriver.exe”))
{
File.Delete(fpath + “//” + “chromedriver.exe”);
File.Copy(fpath + “//” + “chromedriver-win32/chromedriver.exe”, fpath + “//” + “chromedriver.exe”);
}
File.Delete(fpath + “//” + “chromedriver.zip”);
Directory.Delete(fpath + “//” + “chromedriver-win32”, true);
}
catch(Exception E)
{
MessageBox.Show(E.Message + “\r\n” + “Please Refer https://googlechromelabs.github.io/chrome-for-testing/#stable for Downloading”, “Error Processing Chromedriver: “, MessageBoxButton.OK,MessageBoxImage.Stop);
}
Leave a Reply