大家在使用selenium时都遇到过浏览器打开没有加载插件,原因是WebDriver每次启动一个Firefox的实例时,会生成一个匿名的profile,并不会使用当前Firefox的profile。这点一定要注意。
可以看到我的有这么多profile
要解决这个问题 cmd 运行 安装目录\firefox.exe - ProfileManager -no-remote
在新建启动的Firefox中加入相应插件,定义此profile为“selenium”
原先的
WebDriver driver = new FirefoxDriver()
改为
- ProfilesIni allProfiles = new ProfilesIni();
- // "Selenium" is the new profile just created
- FirefoxProfile profile = allProfiles.getProfile("Selenium");
- WebDriver driver = new FirefoxDriver(profile);