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

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

Java代码
  1. import org.openqa.selenium.By; 
  2. import org.openqa.selenium.WebDriver; 
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.firefox.FirefoxDriver; 
  5. import org.openqa.selenium.support.ui.ExpectedCondition; 
  6. import org.openqa.selenium.support.ui.WebDriverWait; 
  7.  
  8. public class Selenium2Example  { 
  9.     public staticvoid main(String[] args) { 
  10.         // Create a new instance of the Firefox driver 
  11.         // Notice that the remainder of the code relies on the interface, 
  12.         // not the implementation. 
  13.         WebDriver driver = new FirefoxDriver(); 
  14.  
  15.         // And now use this to visit Google 
  16.         driver.get("http://www.google.com"); 
  17.         // Alternatively the same thing can be done like this 
  18.         // driver.navigate().to("http://www.google.com"); 
  19.  
  20.         // Find the text input element by its name 
  21.         WebElement element = driver.findElement(By.name("q")); 
  22.  
  23.         // Enter something to search for 
  24.         element.sendKeys("Cheese!"); 
  25.  
  26.         // Now submit the form. WebDriver will find the form for us from the element 
  27.         element.submit(); 
  28.  
  29.         // Check the title of the page 
  30.         System.out.println("Page title is: " + driver.getTitle()); 
  31.          
  32.         // Google's search is rendered dynamically with JavaScript. 
  33.         // Wait for the page to load, timeout after 10 seconds 
  34.         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
  35.             public Boolean apply(WebDriver d) { 
  36.                 return d.getTitle().toLowerCase().startsWith("cheese!"); 
  37.             } 
  38.         }); 
  39.  
  40.         // Should see: "cheese! - Google Search" 
  41.         System.out.println("Page title is: " + driver.getTitle()); 
  42.          
  43.         //Close the browser 
  44.         driver.quit(); 
  45.     } 
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的值,如

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

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

Java代码
  1. File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe"); 
  2. FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary); 
  3. WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。 
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()

方法第一句

Java代码
  1. String binaryName = System.getProperty("webdriver.firefox.bin"); 
String binaryName = System.getProperty("webdriver.firefox.bin");

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值