Reputation: 785
I am trying to write a selenium script that can work without needing any updates. I'd like to be able to run the program, and determine which Google Chrome version is currently installed on OS (it can be either Windows or Linux), and then install the compatible ChromeDriver from there.
I already tried this to simply attempt printing the value:
public static void chromeVersion() throws IOException {
String installPath = "";
Process userProcess;
BufferedReader usersReader;
if(SystemUtils.IS_OS_WINDOWS) {
installPath = "C:/Program Files/Google/Chrome/Application/chrome.exe";
userProcess = Runtime.getRuntime().exec(installPath + " --version");
usersReader = new BufferedReader(new InputStreamReader(userProcess.getInputStream()));
String p;
while ((p = usersReader.readLine()) != null){
System.out.println(p);
}
}
}
But it prints a runtime error saying that the path cannot be found. Even if the path was correct, I doubt that this is the best solution since the path technically could vary even from one Windows computer to another.
What else can I do to accomplish this task?
EDIT: after further research, it seems that this may not be possible in Java? Thoughts?
EDIT: Hulk in the comments pointed out that I can do this:
public static void chromeVersion() throws IOException {
String installPath = "";
Process userProcess;
BufferedReader usersReader;
if(SystemUtils.IS_OS_WINDOWS) {
installPath = "reg query 'HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon' /v version";
;
userProcess = Runtime.getRuntime().exec(installPath);
usersReader = new BufferedReader(new InputStreamReader(userProcess.getInputStream()));
String p;
while ((p = usersReader.readLine()) != null){
System.out.println(p);
}
}
}
This however does not print anything, but if I run reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version
from CMD then I get this:
HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon
version REG_SZ 93.0.4577.82
Upvotes: 1
Views: 4102
Reputation: 785
I managed to get it to work like this:
public static void chromeVersion() throws IOException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("reg query " + "HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon " + "/v version");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
This will print:
Here is the standard output of the command:
HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon
version REG_SZ 93.0.4577.82
Here is the standard error of the command (if any):
Upvotes: 1