2017-03-14 64 views
0

我想用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();和进一步从不执行。 下面是我想要达到的最简单的代码版本:浏览器打开,但永远不会去使用Selenium的URL

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/

+0

你得到一个错误,或者不它只是不去网站?我假设你已经完成了这段代码,看看'driver.quit()'被调用之前会发生什么。 – mrfreester

+0

我用简单的sout()检查过,“new FirefoxDriver”之后的代码从不执行。 –

+1

我相信Narendra的回答是你的问题。你必须下载'geckodriver'并指向那个,而不是'firefox.exe'。基本上,您的代码正在运行'firefox.exe',但不知道如何驱动它。 – mrfreester

回答

3

其实你需要设置geckodriver.exe路径,而不是firefox.exe这个

System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); 

所以只需更换geckodriver.exe路径,并尝试

+0

你说得对,我从这里下载了最新版的geckodriver:https://github.com/mozilla/geckodriver/releases –

相关问题