【问题标题】:How to get the version number of Google Chrome installed on OS?如何获取操作系统上安装的谷歌浏览器的版本号?
【发布时间】:2021-11-26 17:28:50
【问题描述】:

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

我已经尝试过这个只是尝试打印值:

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

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

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

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

编辑:cmets 中的绿巨人指出我可以这样做:

    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

【问题讨论】:

  • 好收获。我在 CMD 中对此进行了测试,即使我认为这是正确的默认路径,仍然找不到路径
  • 这似乎很棘手,过去改过几次,有chrome和windows的版本......
  • 这似乎在命令行中工作正常,我只需要在弄清楚如何在 java 中获取该值之后解析返回的值。

标签: java google-chrome


【解决方案1】:

我设法让它像这样工作:

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

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 2011-12-01
    • 2017-12-14
    相关资源
    最近更新 更多