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

本文介绍了如何使用Selenium WebDriver进行网页自动化操作,并解决了在特定环境下运行示例代码时遇到的Firefox路径配置问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习webdriver,顺便把遇到的问题记在这里,以便日后查阅,并分享给遇到相同问题的人。

 

问题:运行seleniumhq.org网站上的例子。

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

 

 

报如下错误

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的值,如

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

             方法3、 用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法,直接上代码

File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。

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

 

 

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

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

 方法第一句

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

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

Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html ================================================ 08-13-2025 04:06:59 PM Test Cases/Get Report in CWS Elapsed time: 3.477s Test Cases/Get Report in CWS FAILED. Reason: com.kms.katalon.core.exception.StepFailedException: Unable to open browser with url: '' at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:117) at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:43) at com.kms.katalon.core.webui.keyword.builtin.OpenBrowserKeyword.openBrowser(OpenBrowserKeyword.groovy:47) at com.kms.katalon.core.webui.keyword.builtin.OpenBrowserKeyword.execute(OpenBrowserKeyword.groovy:33) at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74) at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.openBrowser(WebUiBuiltInKeywords.groovy:62) at Get Report in CWS.run(Get Report in CWS:62) at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194) at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119) at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448) at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439) at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418) at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410) at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:137) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:125) at TempTestCase1755072413647.run(TempTestCase1755072413647.groovy:25) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created from unknown error: cannot find Chrome binary Host info: host: 'CNM62C3LR3', ip: '10.242.162.85' Build info: version: '4.22.0', revision: 'c5f3146703' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.14' Driver info: com.kms.katalon.selenium.driver.CChromeDriver Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], extensions: []}, unhandledPromptBehavior: ignore, webSocketUrl: true}]}] at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:114) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:75) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:61) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:162) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:161) at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:114) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:72) at com.kms.katalon.selenium.driver.CChromeDriver.<init>(CChromeDriver.java:13) at com.kms.katalon.core.webui.driver.chrome.ChromeDriverBuilder.createDriver(ChromeDriverBuilder.java:41) at com.kms.katalon.core.webui.driver.AbstractDriverBuilder.build(AbstractDriverBuilder.java:39) at com.kms.katalon.core.webui.driver.DriverFactory.startNewBrowser(DriverFactory.java:251) at com.kms.katalon.core.webui.driver.DriverFactory.openWebDriver(DriverFactory.java:161) at com.kms.katalon.core.webui.keyword.builtin.OpenBrowserKeyword$_openBrowser_closure1.doCall(OpenBrowserKeyword.groovy:40) at com.kms.katalon.core.webui.keyword.builtin.OpenBrowserKeyword$_openBrowser_closure1.call(OpenBrowserKeyword.groovy) at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:35) ... 18 more
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值