导航

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

Posted on 2017-06-18 20:51  Young哥哥  阅读(118)  评论(0)    收藏  举报

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

 

webdriverfirefoxjavapath

 

问题:运行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 static void 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.        }  
46.    }  

报如下错误

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");  

   方法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);//这里使用这个构造方法。  

  

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

 

 

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

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

 方法第一句

Java代码  

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

  

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