我有 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: The path to the driver executable must be set by the webdriver.chrome.driver system property
请帮助我......
最佳答案
问题是您要初始化 chromedriver 两次,一次是在您发布的第二个类的开始时,然后是在第二个类中调用该方法时在 launchBroswer 方法中初始化一次。当您在 launchBrowser 方法中设置 chromedriver 路径时,第一次调用初始化 chromeDriver 将不会设置该属性
关于java - 驱动程序可执行文件的路径必须由 webdriver.chrome.driver 设置;非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46407232/