WebDriverException Cannot find firefox binary in PATH.的解決方法

最近在學(xué)習(xí)webdriver,順便把遇到的問題記在這里凡人,以便日后查閱撒会,并分享給遇到相同問題的人紧卒。

問題:運(yùn)行seleniumhq.org網(wǎng)站上的例子奸腺。

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

    }  

}  

報(bào)如下錯誤

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'

看這個報(bào)錯應(yīng)該是firefox安裝路徑不是默認(rèn)路徑餐禁。

解決方法:方法1、最簡單的重新安裝firefox到默認(rèn)路徑洋机。

方法2、直接用System.setPropert方法設(shè)置webdriver.firefox.bin的值洋魂,如


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

方法3绷旗、 用FirefoxBinary類和public FirefoxDriver(FirefoxBinary
binary, FirefoxProfile profile)這個構(gòu)造方法,直接上代碼


File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");  

FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);  

WebDriver driver = new FirefoxDriver(firefoxbin,null);//這里使用這個構(gòu)造方法副砍。  

應(yīng)該還可以在環(huán)境變量里面設(shè)置firefox的路徑也可以衔肢,有興趣的可以試一下。

注:有人可能會不知道webdriver.firefox.bin豁翎,可以看一下源碼角骤,其中

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

方法第一句

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

說明默認(rèn)的時(shí)候取的就是這個值,重新設(shè)置一下心剥。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末邦尊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子优烧,更是在濱河造成了極大的恐慌蝉揍,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件畦娄,死亡現(xiàn)場離奇詭異又沾,居然都是意外死亡弊仪,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門杖刷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來励饵,“玉大人,你說我怎么就攤上這事滑燃∫厶” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵不瓶,是天一觀的道長禾嫉。 經(jīng)常有香客問我,道長蚊丐,這世上最難降的妖魔是什么熙参? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮麦备,結(jié)果婚禮上孽椰,老公的妹妹穿的比我還像新娘。我一直安慰自己凛篙,他們只是感情好黍匾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著呛梆,像睡著了一般锐涯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上填物,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天纹腌,我揣著相機(jī)與錄音,去河邊找鬼滞磺。 笑死升薯,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的击困。 我是一名探鬼主播涎劈,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼阅茶!你這毒婦竟也來了蛛枚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤脸哀,失蹤者是張志新(化名)和其女友劉穎坤候,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體企蹭,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡白筹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年智末,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片徒河。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡系馆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出顽照,到底是詐尸還是另有隱情由蘑,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布代兵,位于F島的核電站尼酿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏植影。R本人自食惡果不足惜裳擎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望思币。 院中可真熱鬧鹿响,春花似錦、人聲如沸谷饿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽博投。三九已至绸贡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間毅哗,已是汗流浹背听怕。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留黎做,地道東北人叉跛。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓松忍,卻偏偏與公主長得像蒸殿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鸣峭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容