"WebDriverException: Cannot find firefox binary in PATH."的解决方法

最近在学习webdriver,顺便把遇到的问题记在这里,以便日后查阅,并分享给遇到相同问题的人。

 

问题:运行seleniumhq.org网站上的例子。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

 

 

报如下错误

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP
Build info: version: '2.18.0', revision: '15704', time: '2012-01-27 17:37:17'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23'

 

看这个报错应该是firefox安装路径不是默认路径。

解决方法:方法1、最简单的重新安装firefox到默认路径。哈哈

             方法2、直接用System.setPropert方法设置webdriver.firefox.bin的值,如

System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");

             方法3、 用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法,直接上代码

File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。

  应该还可以在环境变量里面设置firefox的路径也可以,有兴趣的可以试一下。

 

 

  注:有人可能会不知道webdriver.firefox.bin,可以看一下源码,其中

 org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromSystemProperty()

 方法第一句

String binaryName = System.getProperty("webdriver.firefox.bin");

说明默认的时候取的就是这个值,重新设置一下。

你可能感兴趣的:(java,Path,firefox,webdriver)