【发布时间】:2017-03-14 16:23:40
【问题描述】:
我想用 Selenium 和 jUnit4 为网页编写几个测试,但我不知道如何让 Firefox 打开我需要的 URL。没有 System.setProperty(...) 我得到Exception in thread "main" 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. 并且浏览器永远不会打开。然而,如果我实现它浏览器确实会在默认启动“新页面”上打开,但driver = new FirefoxDriver(); 行和进一步永远不会执行。
下面是我想要实现的最简单的代码版本:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
static String URL = "http://www.google.com";
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//Following code never executes
driver = new FirefoxDriver();
//I'm not sure if this is how I'm supposed to open URL, but I never had this code executed.
driver.get(URL);
driver.quit();
}
}
更新: 这些链接有助于解决正确安装 geckodriver 的问题。 https://github.com/mozilla/geckodriver/releases
http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
【问题讨论】:
-
您是否收到错误消息,或者它只是没有进入该站点?我假设您已经通过此代码查看在调用
driver.quit()之前会发生什么 -
我用简单的 sout() 检查过,“new FirefoxDriver”之后的代码永远不会执行。
-
我相信纳伦德拉的答案是你的问题。您必须下载
geckodriver并指向它,而不是firefox.exe。基本上,您的代码正在运行firefox.exe,但不知道如何驱动它。 -
确保 FF 和 selenium 驱动程序都是最新的并且您已正确安装它们。此行为表明版本不匹配或安装错误。
-
@JeffC 我正在运行两者的最新版本
标签: java selenium firefox junit