我是Selenium的新手,不能运行最简单的程序:
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
HelloWorld.LOGGER.info(driver.getTitle());
}
我收到的错误:
Exception in thread "main" 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
at com.google.common.base.Preconditions.checkState(Preconditions.java:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at com.genesys.testing.topicsDefinitionUI.HelloWorld.main(HelloWorld.java:20)
在阅读了上面的异常之后,我在这个网站上看到了一个对我不起作用的解决方案:
public static void main(String[] args) {
System.setProperty("WebDriver.Chrome.driver","/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
HelloWorld.LOGGER.info(driver.getTitle());
}
我收到的错误:
Exception in thread "main" 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
at com.google.common.base.Preconditions.checkState(Preconditions.java:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at com.genesys.testing.topicsDefinitionUI.HelloWorld.main(HelloWorld.java:20)
我在MacOS-Catalina上工作,我的IDE是Intellij,这是一个Maven项目。
尝试将System.setProperty()
函数的第二个参数切换到路径的许多变体,但没有成功。
我错过了什么?
要在chrome浏览器上运行代码,您需要设置chromedriver的路径。ypu haavee在你的代码中指定了chrome驱动程序的错误路径
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
System.out.println(driver.getTitle());
检查您的浏览器版本,并从以下链接下载chromedriver:
https://chromedriver.chromium.org/downloads
所以,我在@Rock上面的朋友是对的。
我确实需要下载chrome驱动程序。
但是,因为我在macOS Catalina上工作,我不得不手动更改chromedriver文件的权限。
在这个问题的帮助下:MacOS Catalina(v 10.15.3(:错误:"chromedriver"无法打开,因为无法验证开发人员。无法启动chrome浏览器
这就是代码现在的样子:
public void firstTest() {
System.setProperty("webdriver.chrome.driver","/Users/raziv/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
System.out.println(driver.getTitle());
}
运行良好