You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium初始化FirefoxDriver禁用位置通知遇构造函数错误

Fixing FirefoxDriver Constructor Error with FirefoxProfile

Hey there! The error you're seeing happens because recent versions of Selenium (3.8+) have deprecated passing a FirefoxProfile directly to the FirefoxDriver constructor. Instead, you need to wrap your profile settings inside a FirefoxOptions object, which is the standard way to configure FirefoxDriver now.

Why the Error Occurs

Looking at your error message, the FirefoxDriver constructor no longer accepts a FirefoxProfile as an argument—it expects FirefoxOptions, Capabilities, or a driver service instead. Your code is trying to pass a FirefoxProfile directly, which causes the type mismatch.

Corrected Code

Here's how to adjust your code to work with the current Selenium API, including the fix for disabling location permission requests (I noticed your original code set javascript.enabled to false—if that was a detour from your location permission goal, I've added the correct preferences below):

System.setProperty(Utility.MOZILLA_DRIVER, Utility.MOZILLA_PATH);

// Create FirefoxProfile and set your preferences
FirefoxProfile ffprofile = new FirefoxProfile();
// Disable location permission prompts (your original goal)
ffprofile.setPreference("geo.enabled", false);
ffprofile.setPreference("geo.provider.use_corelocation", false);
ffprofile.setPreference("geo.prompt.testing", false);
ffprofile.setPreference("geo.prompt.testing.allow", false);
// Keep this line if you actually need to disable JavaScript
// ffprofile.setPreference("javascript.enabled", false);

// Wrap the profile in FirefoxOptions (required for modern Selenium)
FirefoxOptions options = new FirefoxOptions();
options.setProfile(ffprofile);

// Initialize FirefoxDriver with the configured options
driver = new FirefoxDriver(options);
driver.manage().window().maximize();

Your Utility class can stay exactly as it is:

public class Utility {
    public static final String MOZILLA_PATH="C:\\SeleniumDrivers\\firefox\\geckodriver.exe";
    public static final String MOZILLA_DRIVER="webdriver.gecko.driver";
}

Key Changes

  • We create a FirefoxOptions instance and attach the FirefoxProfile to it using options.setProfile(ffprofile)
  • We pass options to the FirefoxDriver constructor instead of the profile directly
  • Added the correct preferences to turn off location permission prompts, which aligns with your original initialization goal

内容的提问来源于stack exchange,提问作者erdi.p

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:37:46