Intellij Idea提示cannot resolve constructor,自定义Firefox路径方案不生效
错误根因
你使用的Selenium版本较新(Selenium 3.141.x后续版本及Selenium 4+全系列)已经废弃了直接传入FirefoxBinary和FirefoxProfile的FirefoxDriver构造方法,因此会抛出无法解析构造函数的报错。
解决方案
改用FirefoxOptions封装所有火狐浏览器配置项,再将配置传入FirefoxDriver构造方法即可,修改后的代码如下:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import java.io.File; public class firefoxDriver { public static void main(String[] args) { // 配置火狐非默认安装路径 File pathBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe"); FirefoxBinary ffBinary = new FirefoxBinary(pathBinary); FirefoxProfile ffProfile = new FirefoxProfile(); // 配置geckodriver路径 System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe"); // 用FirefoxOptions封装所有配置 FirefoxOptions options = new FirefoxOptions(); options.setBinary(ffBinary); options.setProfile(ffProfile); // 传入配置创建驱动对象 WebDriver ffDriver = new FirefoxDriver(options); ffDriver.get("https://google.com"); System.out.println(ffDriver.getTitle()); } }
注意事项
- 如果后续还要添加其他浏览器启动参数,统一通过
FirefoxOptions的对应方法配置即可 - 要保证geckodriver版本和你安装的Firefox浏览器版本、Selenium版本三者兼容
内容的提问来源于stack exchange,提问作者Yekta Özdemir
相关产品推荐
相关产品推荐

