我想使用 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/
最佳答案
实际上你需要在此设置geckodriver.exe
路径而不是firefox.exe
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
所以只需替换 geckodriver.exe
路径并尝试
关于java - 浏览器打开,但从未使用 Selenium 访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791332/