如何获取安装在OS上的GoogleChrome的版本号?

How to get the version number of Google Chrome installed on OS?

我正在尝试编写一个无需任何更新即可运行的 selenium 脚本。我希望能够 运行 程序,并确定 Google Chrome 当前安装在 OS 上的版本(它可以是 Windows 或Linux),然后从那里安装兼容的 Chrome驱动程序。

我已经试过这个来简单地尝试打印值:

    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);
            }
        }
        
    }

但是它打印 运行time 错误,说找不到路径。即使路径是正确的,我也怀疑这是最好的解决方案,因为从技术上讲,即使从一台 Windows 计算机到另一台计算机,路径也可能不同。

我还能做些什么来完成这个任务?

编辑:经过进一步研究,这似乎在 Java 中是不可能的?想法?

编辑:绿巨人在评论中指出我可以做到这一点:

    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);
            }
        }
        
    }

但是这不会打印任何东西,但是如果我从 CMD 运行 reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version 然后我得到这个:

HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon
    version    REG_SZ    93.0.4577.82

我设法让它像这样工作:

    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);
            }
        
    }

这将打印:

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):