首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取安装在操作系统上的Google Chrome的版本号?

如何获取安装在操作系统上的Google Chrome的版本号?
EN

Stack Overflow用户
提问于 2021-10-06 14:08:11
回答 1查看 86关注 0票数 1

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

我已经尝试过了,只是简单地尝试打印值:

代码语言:javascript
运行
复制
    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中似乎是不可能的?有什么想法?

编辑:绿巨人在评论中指出,我可以这样做:

代码语言:javascript
运行
复制
    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,则会得到以下结果:

代码语言:javascript
运行
复制
HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon
    version    REG_SZ    93.0.4577.82
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-06 14:44:03

我设法让它像这样工作:

代码语言:javascript
运行
复制
    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);
            }
        
    }

这将打印以下内容:

代码语言:javascript
运行
复制
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):
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69467318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档