如何使Firefox驱动程序与Firefox手动打开相同

时间:2014-11-13 17:24:34

标签: java firefox selenium selenium-webdriver

我注意到使用Selenium Webdriver打开带有配置文件的Firefox与使用相同的配置文件手动打开Firefox有许多不同之处。主页不会加载到Selenium Webdriver驱动程序中,about:config中的非布尔设置无法修改...以命名一些差异。有没有办法让Selenium Webdriver打开Firefox驱动程序,就像你手动打开Firefox一样?

编辑:这是我目前用于完整性检查的代码......

        File profileDirectory = new File("C:\\Users\\[UserName]\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\cox74xm7.default");
        FirefoxProfile ffprofile = new FirefoxProfile(profileDirectory);
        WebDriver ffdriver = new FirefoxDriver(ffprofile);

2 个答案:

答案 0 :(得分:1)

您需要实例化FirefoxProfile并将其传递给WebDriver构造函数:

File profileDirectory = new File(path);
FirefoxProfile profile = new FirefoxProfile(profileDirectory);
WebDriver webDriver = new FirefoxDriver(profile);

其中path是您现有个人资料的路径。

答案 1 :(得分:1)

FirefoxDriver永远不会使用配置文件直接启动浏览器,即使使用FirefoxProfile对象通过构造函数传入也是如此。配置文件的副本始终由驱动程序创建。原因是驱动程序必须考虑使用驱动程序的用户使用相同配置文件执行多个Firefox实例的用例。使用实际配置文件 in situ 会因为显而易见的原因而出现问题。将现有配置文件传递给驱动程序构造函数时,应将整个配置文件复制到临时目录中,并根据需要使用它。但请注意,有些配置文件设置必须设置某些设置才能使驱动程序正常运行。