我想写一个硒脚本,可以不需要任何更新的工作。我希望能够运行该程序,并确定当前安装在操作系统上的谷歌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计算机到另一台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_USERSoftwareGoogleChromeBLBeacon" /v version
,那么我得到这个:
HKEY_CURRENT_USERSoftwareGoogleChromeBLBeacon
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_USERSoftwareGoogleChromeBLBeacon
version REG_SZ 93.0.4577.82
Here is the standard error of the command (if any):