必须通过webdriver.chrome.driver设置驱动程序可执行文件的路径; IllegalStateException异常

时间:2017-09-25 14:07:56

标签: java selenium-webdriver illegalstateexception

我有2个包裹。在一个包中,我声明了launchBrowser()方法,如下所示:

package BaseCode;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver;

public class TestBase {

    public static WebDriver driver = null;

        public void launchBrowser(String baseUrl) throws Exception{
            try {
                    System.out.println("Launching the Chrome Browser");
                    String driverpath = "E:\\Learning\\Selenium\\Drivers\\ChromeDriver\\chromedriver.exe";
                    System.setProperty("webdriver.chrome.driver",driverpath);
                    driver = new ChromeDriver();
                    driver.manage().window().maximize();
                    System.out.println("Opening URL: " + baseUrl);
                    driver.get(baseUrl);
        }catch(Exception E) {
            System.out.println(E.getMessage() +"\n" + E.getStackTrace());
            }
        }
}

我调用上述launchBrowser()方法的第二个类是:

package pack_one;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import BaseCode.TestBase;

public class MultipleiFrames {

    public static TestBase B = new TestBase();

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();
        String url = "http://demo.guru99.com/selenium/guru99home/";
        try {
            B.launchBrowser(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

运行代码时,我在线程" main"

中获得异常
  

java.lang.IllegalStateException:驱动程序可执行文件的路径   必须由webdriver.chrome.driver系统属性

设置

请帮助我....

2 个答案:

答案 0 :(得分:1)

问题是你正在初始化chromedriver两次,一次是在你发布的第二个类的开始,然后再次在launchBroswer方法中,当你在第二个类中调用它。当您在launchBrowser方法中设置chromedriver路径时,第一次初始化chromeDriver的调用将不具有该属性集

答案 1 :(得分:0)

这是一个普遍的问题,请确保您只需要初始化类一次,否则会发生此异常。

只需删除一个驱动程序初始化声明,其余就可以了

driver = new ChromeDriver();
相关问题