如何使用Java获取名称和默认浏览器版本?
我需要使用java编写3个方法来确定浏览器在3个操作系统中使用的默认值:Windows、Linux和Mac。 我想,我需要像这样使用windows注册表 但是在Linux中如何做到这一点呢? 我需要像这样的东西如何使用Java获取名称和默认浏览器版本?,java,selenium,Java,Selenium,我需要使用java编写3个方法来确定浏览器在3个操作系统中使用的默认值:Windows、Linux和Mac。 我想,我需要像这样使用windows注册表 但是在Linux中如何做到这一点呢? 我需要像这样的东西 System.out.println(getBrowserForLinux()) //Chrome System.out.println(getBrowserVersionForLinux()) //79 您可以修改此代码(使用selenium)以获得所需的内容 import org.
System.out.println(getBrowserForLinux())
//Chrome
System.out.println(getBrowserVersionForLinux())
//79
您可以修改此代码(使用selenium)以获得所需的内容
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class BrowserVersion {
private static WebDriver browserDriver;
public static String getBrowserAndVersion() {
String browser_version = null;
Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities();
String browsername = cap.getBrowserName();
// This block to find out IE Version number
if (“internet explorer”.equalsIgnoreCase(browsername)) {
String uAgent = (String) ((JavascriptExecutor) browserDriver).executeScript(“return navigator.userAgent;”);
System.out.println(uAgent);
//uAgent return as “MSIE 8.0 Windows” for IE8
if (uAgent.contains(“MSIE”) && uAgent.contains(“Windows”)) {
browser_version = uAgent.substring(uAgent.indexOf(“MSIE”)+5, uAgent.indexOf(“Windows”)-2);
} else if (uAgent.contains(“Trident/7.0”)) {
browser_version = “11.0”;
} else {
browser_version = “0.0”;
}
} else
{
//Browser version for Firefox and Chrome
browser_version = cap.getVersion();// .split(“.”)[0];
}
String browserversion = browser_version.substring(0, browser_version.indexOf(“.”));
return browsername + ” ” + browserversion;
}
public static String OSDetector () {
String os = System.getProperty(“os.name”).toLowerCase();
if (os.contains(“win”)) {
return “Windows”;
} else if (os.contains(“nux”) || os.contains(“nix”)) {
return “Linux”;
}else if (os.contains(“mac”)) {
return “Mac”;
}else if (os.contains(“sunos”)) {
return “Solaris”;
}else {
return “Other”;
}
}
}
试试这个。你的问题上有一个硒标签,但你没有在问题中说明任何类似的内容。请明确您是否正在使用selenium。是否可以在启动驱动程序之前确定浏览器版本?我猜是在init方法中。