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

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

C:\Users\HUAWEI\Desktop>python bilibili_danmu.py Traceback (most recent call last): File "C:\Users\HUAWEI\Desktop\bilibili_danmu.py", line 15, in <module> driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ browser_name=DesiredCapabilities.CHROME["browserName"], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... keep_alive=keep_alive, ^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 69, in __init__ super().__init__(command_executor=executor, options=options) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 261, in __init__ self.start_session(capabilities) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 362, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 454, in execute self.error_handler.check_response(response) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "D:\AppGallery\Downloads\python\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary Stacktrace: Backtrace: GetHandleVerifier [0x0090A813+48355] (No symbol) [0x0089C4B1] (No symbol) [0x007A5358] (No symbol) [0x007C1A9E] (No symbol) [0x007C0579] (No symbol) [0x007F0C55] (No symbol) [0x007F093C] (No symbol) [0x007EA536] (No symbol) [0x007C82DC] (No symbol) [0x007C93DD] GetHandleVerifier [0x00B6AABD+2539405] GetHandleVerifier [0x00BAA78F+2800735] GetHandleVerifier [0x00BA456C+2775612] GetHandleVerifier [0x009951E0+616112] (No symbol) [0x008A5F8C] (No symbol) [0x008A2328] (No symbol) [0x008A240B] (No symbol) [0x00894FF7] BaseThreadInitThunk [0x772B5D49+25] RtlInitializeExceptionChain [0x77BECF0B+107] RtlGetAppContainerNamedObjectPath [0x77BECE91+561]还是这样,该怎么解决呢?请告诉我比较详细的操作方案,我安装上Firefox需要进行环境配置吗?
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值