Java代码如下(环境:eclipse Oxygen+JDK1.8+selenium3.8.1+Junit5+firefox48.0.2)
1.报错信息如下
The path to the driver executable must be set by the webdriver.gecko.driver system property;
for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
1 java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases 2 at com.google.common.base.Preconditions.checkState(Preconditions.java:754) 3 at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) 4 at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41) 5 at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:141) 6 at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339) 7 at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:158) 8 at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120) 9 at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98) …………
部分源码如下:
@Test public void setUp() throws Exception { try { driver = new FirefoxDriver(); driver.get("https://www.baidu.com/"); System.out.print(driver.getTitle()); driver.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.解决方法如下:
根据报错提示意为,缺少驱动。
(1)根据提示链接:https://github.com/mozilla/geckodriver/releases
下载最新驱动
(2)解压下载文件,把解压后的文件放入firefox.exe的同目录,默认为 C:\Program Files (x86)\Mozilla Firefox
(3)在源码中的加入
System.setProperty("webdriver.firefox.marionette", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
如下:
1 @Test 2 public void setUp() throws Exception { 3 4 try { 5 System.setProperty("webdriver.firefox.marionette", 6 "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe"); 7 driver = new FirefoxDriver(); 8 driver.get("https://www.baidu.com/"); 9 System.out.print(driver.getTitle()); 10 driver.quit(); 11 } catch (Exception e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 }
3.ok,解决了!(其他问题待补充)