使用Selenium初始化FirefoxDriver禁用位置通知遇构造函数错误
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
FirefoxOptionsinstance and attach theFirefoxProfileto it usingoptions.setProfile(ffprofile) - We pass
optionsto theFirefoxDriverconstructor 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

