2017-02-08 92 views
0

我有一個使用Java的Selenium項目,使用不同的瀏覽器。我正在嘗試引入MS Edge,但無法使用Capabilities中的getVersion()方法。以下是初始化瀏覽器的方法的代碼片段。 WebDriver「驅動程序」在課程開始時聲明。MS邊緣:無法通過Capabilities.getVersion()獲取瀏覽器版本

  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調試,當我懸停對其進行初始化後的 '帽' 物體上時,它顯示我以下: 能力[{acceptSslCerts =真,browserVersion = 25.10586.672.0,platformVersion = 10 ,browserName = MicrosoftEdge,takesScreenshot = true,pageLoadStrategy = normal,takesElementScreenshot = true,platformName = windows,platform = ANY}]

字符串browserName爲「MicrosoftEdge」,但browserVersion爲空。我期望它會出來是「25.10586.672.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; 
相關問題