2017-01-23 166 views
0

似乎可以使此代碼執行。編譯器沒有實例化驅動程序。我能做些什麼來糾正這一點。無法實例化類型FirefoxDriver

package mypackage; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

org.openqa.selenium.support.ui.ExpectedConditions; 
import 

org.openqa.selenium.support.ui.WebDriverWait; 

public class selenium { 

    public static void main(String[] args) { 
     WebDriver driver = (WebDriver) new FirefoxDriver(); 
     WebDriverWait MyWaitlVar= new 

WebDriverWait(driver, 10); 
     String baseUrl = 

"http://newtours.demoaut.com"; 
     String expectedTitle = "Welcome: Mercury Tours"; 
     String actualTitle = ""; 

     // launch Firefox and direct it to the Base URL 
     driver.get(baseUrl); 

     // get the actual value of the title 
     actualTitle = driver.getTitle(); 

     /* 
     * compare the actual title of the page 

witht the expected one and print 
     * the result as "Passed" or "Failed" 
     */ 
     if (actualTitle.contentEquals 

(expectedTitle)){ 
      System.out.println("Test Passed!"); 
     } else { 
      System.out.println("Test Failed"); 
     } 

     //close Firefox 
     driver.close(); 

     // exit the program explicitly 
     System.exit(0); 
    } 

} 
+3

什麼是編譯器錯誤? –

+0

異常在線程「主要」 java.lang.Error的:未解決問題彙編: \t不能在mypackage.myclass.main實例化類型FirefoxDriver \t(myclass.java:16) – Ken

+0

嘗試使用在[這個]的例子( http://www.seleniumhq.org/docs/03_webdriver.jsp)網站。 –

回答

0

網站www.seleniumhq.org詳細說明如何使用該驅動程序。請參閱部分通過示例介紹Selenium-WebDriver API。創建一個project文件

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>MySel20Proj</groupId> 
    <artifactId>MySel20Proj</artifactId> 
    <version>1.0</version> 
    <dependencies> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-server</artifactId> 
     <version>3.0.1</version> 
    </dependency> 
    </dependencies> 
</project> 

,並使用MVN全新安裝。也示出了代碼示例(支持多種語言):

package org.openqa.selenium.example; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class Selenium2Example { 
    public static void main(String[] args) { 
     WebDriver driver = new FirefoxDriver(); 

     driver.get("http://www.google.com"); 
     WebElement element = driver.findElement(By.name("q")); 

     element.sendKeys("Cheese!");  
     element.submit(); 

     System.out.println("Page title is: " + driver.getTitle()); 

     // Google's search is rendered dynamically with JavaScript. 
     // Wait for the page to load, timeout after 10 seconds 
     (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver d) { 
       return d.getTitle().toLowerCase().startsWith("cheese!"); 
      }  
     }); 

     System.out.println("Page title is: " + driver.getTitle());    
     driver.quit(); 
    } 
} 

所以這應該工作:

  • 檢查或更新您的當前設置。
  • 使用上面的例子來測試使用Google網站的驅動程序。
+0

感謝您的信息。 – Ken

+0

@Ken請接受此答案(綠色選中標記) –