初次使用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)