在Eclipse IDE中执行Selenium脚本时出错

时间:2019-03-04 01:23:02

标签: java selenium

我收到以下脚本的以下错误。我已将所有jar文件添加到库中。

============================================================
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    WebDriver cannot be resolved to a type
    FirefoxDriver cannot be resolved to a type

    at webdriver/Demo.Sample.main(Sample.java:11)
=============================================================
package Demo;

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

public class Sample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hello");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
    }
}

1 个答案:

答案 0 :(得分:0)

似乎您导入了错误的jar文件。从https://selenium-release.storage.googleapis.com/index.html?path=2.53/下载selenium-server-standalone-2.53.1.jar文件,并将其导入到eclipse中并删除其他jar文件。

由于您尝试使用Selenium WebDriver自动化Firefox,因此还需要下载Firefox可执行二进制文件。根据您的浏览器版本,您可以从https://github.com/mozilla/geckodriver/

获取gecko驱动程序

并在驱动程序初始化之前在代码中添加以下行,并提供具有名称和范围的Firefox可执行二进制文件的绝对路径:

System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable");

下面是修改后的代码:

package Demo;

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

public class Sample {

    public static void main(String[] args) {
        System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable\\geckodriver.exe"); // Provide your system path to the gecko driver with name and extension here
        System.out.println("hello");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
    }
}

希望对您有帮助...