2017-10-15 67 views
-1

我使用的基本代码如下:webdriver的运行硒时“FirefoxDriver不能被解析为一个类型”

Package TestSelenium; 
import org.openqa.selenium.WebDriver; 
public class MyFirstClass { 
    public static void main(String[] args) { 
     WebDriver driver=new FirefoxDriver(); 
     driver.get("http://www.google.com") 
    } 
} 

不过,我得到错误

FirefoxDriver不能得到解决到一个类型

我已经包括所有必需的罐子,但仍然,我收到此错误。

我使用硒3.60:

C:\Users\Ankur>javac -version 

的javac 1.8.0_144

截图所有必需的jar文件:

Selenium jars

回答

0

您需要添加在顶部以下import语句的页面

import org.openqa.selenium.firefox.FirefoxDriver; 

它仍然会抛出一些异常,因为从硒3.X的病房,你不能直接启动Firefox浏览器。您需要从selenium.hq.org下载浏览器驱动程序,并且在您的代码中,您需要使用System.setProperties方法或期望的功能类来指定浏览器驱动程序可用的位置。

+0

即使在更改代码后,我仍然得到错误: import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox。FirefoxDriver; System.setProperty(“webdriver.firefox.marionette”,“C:\\ Users \\ Ankur \\ Downloads \\ geckodriver-v0.19.0-win64 \\ geckodriver.exe”); \t WebDriver Driver = new FirefoxDriver(); \t Driver.get(“https://www.google.co.in/”); } –

+0

使用此代码,它将工作---- System.setProperty(“webdriver.gecko.driver”,“C:\\ SeleniumTraining \\ BrowserDriver \\ geckodriver.exe”); //你的exe文件的路径 WebDriver driver = new FirefoxDriver(); //打开浏览器 driver.get(“http://google.com");//在firefox中打开google页面 System.out.println (driver.getTitle()); //打印标题 –

+0

仍然遇到原始问题。 –

0

您看到的错误FirefoxDriver cannot be resolved to a type这么说。这意味着您正在使用的IDE,即Eclipse无法解析关键字FirefoxDriver

正如您从共享的快照中看到的那样,关键字FirefoxDriver下划线用红线表示缺少分辨率。原因是我们没有添加所需的importFirefoxDriver定义于org.openqa.selenium.firefox.FirefoxDriver。所以我们也必须导入org.openqa.selenium.firefox.FirefoxDriver

同样,如果你只在你的进口增加org.openqa.selenium.firefox.FirefoxDriver我们仍然会面临几个错误移动头部的,因为我们还没有在我们的代码块中提到的geckodriver二进制即geckodriver.exe的位置。我们需要从这个location下载geckodriver.exe,并将其放置在我们的系统,并通过System.setProperty()提供geckodriver.exe的绝对路径如下:

package TestSelenium; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class MyFirstClass 
{ 
    public static void main(String[] args) throws Exception 
    { 
     System.setProperty("webdriver.gecko.driver", "C:\\your_location\\geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("http://www.google.com"); 
    } 
} 
+0

即使在添加上面的导入语句之后,我也看到了同样的错误。 –

相关问题