无法传递FirefoxProfile参数在webdriver中使用首选项来下载文件

时间:2017-11-30 07:43:06

标签: java selenium mutablecapabilities

public class download {
    public static WebDriver driver;

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver", "/home/ranjith/Downloads/geckodriver");
        //driver = new FirefoxDriver();

        FirefoxProfile profile = new FirefoxProfile();

        profile.setPreference("browser.download.dir", "/home/ranjith/Downloads");
        profile.setPreference("browser.download.folderList", 2);

        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");        
        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );

        driver = new FirefoxDriver(profile); 

        driver.get("http://toolsqa.com/automation-practice-form/");
        driver.findElement(By.linkText("Test File to Download")).click();

        Thread.sleep(5000);

        //driver.close();
    }
}

要求删除参数配置文件以匹配eclipse中的FirefoxDriver 你能帮忙解决这个问题。

此行引发错误

driver = new FirefoxDriver(profile); 

1 个答案:

答案 0 :(得分:2)

根据 FirefoxDriver 类的 Selenium JavaDoc FirefoxDriver(profile)方法不再支持有效Constructor

相反,我们鼓励您使用FirefoxOptions类扩展MutableCapabilities,即 org.openqa.selenium.MutableCapabilities

因此,当您通过driver = new FirefoxDriver(profile);在每次执行时创建新的 FirefoxProfile 时,您必须使用 FirefoxOptions 类中的setProfile()方法其定义为:

public FirefoxOptions setProfile(FirefoxProfile profile)

您的代码块将是:

System.setProperty("webdriver.gecko.driver", "/home/ranjith/Downloads/geckodriver");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "/home/ranjith/Downloads");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");        
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(profile);
driver = new FirefoxDriver(opt);