无法解析FirefoxDriver构造方法求助:加载Firefox配置文件报错
解决FirefoxDriver构造方法无法解析的问题
嘿,我来帮你搞定这个报错!问题出在新版Selenium已经不再支持直接把FirefoxProfile传给FirefoxDriver的构造方法了,你得把配置好的Profile放到FirefoxOptions里,再把Options对象传给驱动构造方法。
看你已经创建了FirefoxOptions并设置了Profile,但最后一步还是传了FirefoxProfile,这就是报错的根源。给你修正后的代码:
// 注意Windows路径的反斜杠要转义成双反斜杠,或者用正斜杠 FirefoxProfile ff_ep_profile = new FirefoxProfile(new File("C:\\Users\\admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\81uy033g.FirefoxEP")); FirefoxOptions options = new FirefoxOptions(); options.setProfile(ff_ep_profile); // 这里要传FirefoxOptions对象,而不是FirefoxProfile WebDriver webDriver = new FirefoxDriver(options);
另外给你一个更简洁的写法(适合Selenium 4+版本),不用手动创建FirefoxProfile对象,直接通过参数指定配置文件路径:
FirefoxOptions options = new FirefoxOptions(); // 直接通过-addArguments指定Profile路径 options.addArguments("-profile", "C:\\Users\\admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\81uy033g.FirefoxEP"); WebDriver webDriver = new FirefoxDriver(options);
这样就能正常加载你的Firefox扩展配置文件,不会再报构造方法无法解析的错误啦~
内容的提问来源于stack exchange,提问作者Nabeel A.Rahman
相关产品推荐
相关产品推荐

