2017-01-23 121 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请接受此答案(绿色选中标记) –

相关问题