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

unable to Pass FirefoxProfile parameter In webdriver to use preferences to download file

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); 

根据 Selenium JavaDoc of FirefoxDriver Class,不再支持 FirefoxDriver(profile) 方法作为有效 Constructor.

相反,鼓励使用扩展 MutableCapabilitiesFirefoxOptions Class,即 org.openqa.selenium.MutableCapabilities

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

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);