java selenium启动火狐浏览器报错:Cannot find firefox binary in PATH. Make sure firefox is installed. OS appear...

Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'

解决方法:

package my_automation;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class open_browser_01 {

    public static void main(String[] args) throws InterruptedException {
        // E:\Program Files (x86)\Mozilla Firefox
        //System.setProperty("webdriver.gecko.driver", "E:\\webdriver\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.baidu.com");
        driver.manage().window().maximize();
        Thread.sleep(5000);
        driver.quit();

    }

}

 

 

 

FirefoxDriver调用firefox浏览器的安装路径应为C盘的默认目录下,若firefox安装在其他目录下执行时会报错: 
Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP 
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54' 
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_10' 
这是因为找不到firefox的原因。 
解决办法: 
1.重新安装firefox在默认路径下; 
OSExpected Location of Firefox
Linuxfirefox (found using "which")
Mac/Applications/Firefox.app/Contents/MacOS/firefox
Windows%PROGRAMFILES%\Mozilla Firefox\firefox.exe

2.直接用System.setProperty方法设置webdriver.firefox.bin的值 
Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.*;  
  5. public class BaiduFirefoxDriver {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  13.         WebDriver driver=new FirefoxDriver();  
  14.         driver.get("http://www.baidu.com/");  
  15.     }  
  16.   
  17. }  

3.利用setCapability进行设置 
Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.*;  
  5. import org.openqa.selenium.remote.DesiredCapabilities;  
  6. public class BaiduFirefoxDriver {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         DesiredCapabilities capability=DesiredCapabilities.firefox();  
  14.         capability.setCapability("firefox_binary",  
  15.         "D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  16.         WebDriver driver = new FirefoxDriver(capability);  
  17.         driver.get("http://www.baidu.com/");  
  18.     }  
  19.   
  20. }  

4.用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法 
Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.openqa.selenium.WebDriver;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.         File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
  15.         FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);    
  16.         WebDriver driver = new FirefoxDriver(firefoxbin,null);  
  17.         driver.get("http://www.baidu.com/");  
  18.     }  
  19.   
  20. }  
 
 

转载于:https://www.cnblogs.com/my-blogs-for-everone/p/8026878.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值