我正在尝试编写一个无需任何更新即可工作的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中似乎是不可能的?有什么想法?
编辑:绿巨人在评论中指出,我可以这样做:
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
发布于 2021-10-06 14:44:03
我设法让它像这样工作:
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):
https://stackoverflow.com/questions/69467318
复制相似问题