我有一个使用Java的Selenium项目,它使用不同的浏览器。我正在尝试引入MS Edge,但无法使用Capabilities中的getVersion()方法。以下是初始化浏览器的方法的代码段。 WebDriver“driver”在类的开头声明。
if(strBrowser.equalsIgnoreCase("Edge"))
{
String FileName=m1.get("TOOLS_PATH").toString()+"//MicrosoftWebDriver.exe";
System.out.println("Full path to Edge executable: " + FileName);
File edgeDriver = new File(FileName);
System.setProperty("webdriver.edge.driver", edgeDriver.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.edge();
driver = new EdgeDriver(caps); //launches Edge browser
}
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion().toString();
在ecliipse中调试期间,当我在初始化后将鼠标悬停在'caps'对象上时,它会显示以下内容: 功能[{acceptSslCerts = true, browserVersion = 25.10586.672.0 ,platformVersion = 10,browserName = MicrosoftEdge,takeScreenshot = true,pageLoadStrategy = normal,takesElementScreenshot = true,platformName = windows,platform = ANY}] < / p>
字符串browserName是“MicrosoftEdge”,但是browserVersion是空的。我希望它能成为“25.10586.672.0”
答案 0 :(得分:0)
尝试以下方法
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(, browser_version.indexOf("."));
return browsername + " " + browserversion;
答案 1 :(得分:0)
在firefox和Edge中,您需要指定需要其值的字段名称-因此请执行以下操作:
Capabilities capabilities = ((RemoteWebDriver) webDriver).getCapabilities();
String browserVersion = capabilities.getCapability("browserVersion").toString();