java - TestNG Selenium Java -- java.lang.IllegalStateException : The path to the driver executable must be set by the webdriver. chrome.driver 系统属性

标签 java selenium

如果我不使用 TestNG 而只使用普通的 Java Selenium,那一切都很好。 但是如果我将 TestNG 与 Java Selenium 一起使用,则会收到此错误。

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

我已经将驱动程序的路径设置为可执行文件,但编译器仍然提示。有什么建议吗?谢谢。

package testSuite;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Driver;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.junit.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class testNG 
{
	
	@Test
	public void login() throws IOException {
		System.setProperty("WebDriver.Chrome.Driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = null; 
		Properties prop = new Properties(); 
		FileInputStream file = new FileInputStream("C:\\Users\\workspace\\Selenium\\src\\testSuite\\config.properties");
		prop.load(file);
		
		System.out.println(prop.getProperty("username"));
		
		if(prop.getProperty("browser").equals("chrome")) {
			System.out.println("OKOK");
			driver = new ChromeDriver(); 
			
		}
		driver.get(prop.getProperty("url"));
		
	}
  }

这是我的属性文件

username = 56987
password = 1234
url = www.google.com
browser = chrome

最佳答案

我发现了这个问题。 只是一个小小的错误。

它应该是小写webdriver.chrome.driver

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");

而不是

System.setProperty("WebDriver.Chrome.Driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");

需要注意小写或大写。谢谢。

关于java - TestNG Selenium Java -- java.lang.IllegalStateException : The path to the driver executable must be set by the webdriver. chrome.driver 系统属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013783/

相关文章:

java - 有什么方法可以通过 JNI 获取指向 Java 数组的直接指针吗?

Selenium-Java 库中缺少 AndroidWebDriver 类

Java - 将泛型限制为内置/不可变类型(整数、短整型等)?

java - 如何在 Windows 上制作相同高度的 SWT 按钮、文本和标签?

selenium - 构建 selenium-server-standalone 可执行 jar

selenium - 如何获得具有相同值的以下img类的xpath?

C# Selenium RC - 如何测试 jQuery 按钮点击?

javascript - 是否可以使用 webdriver.js 观察 postMessages?

java - 方法中内部类的返回值

java - 使用 NetBeans 和 Maven 进行增量构建以进行 jetty 热部署