初次使用selenium +java,出现以下报错java
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40' System info: host: 'ali-wb262234n', ip: '169.254.201.2', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121' Driver info: driver.version: FirefoxDriver at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:74) at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60) at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120) at com.mypro.jase.baidu.main(baidu.java:11)
百度后得知,这个问题是因为firefox的安装路径不是默认的路径引发的,解决方法有三个:web
1.从新安装firefox到默认路径 2.使用System.setPropert方法设置webdriver.firefox.bin的值:
System.setProperty("webdirver.firefox.bin","firefox.exe的安装路径");
3.使用firefox类的构造方法:public FirefoxDriver(FirefoxBinarybinay ,firefoxProfile profile)
File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe"); FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary); WebDriver driver = new FirefoxDriver(firefoxbin,null)