小编典典

无法通过FirefoxProfile参数在webdriver中使用首选项下载文件

selenium

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

阅读 286

收藏
2020-06-26

共1个答案

小编典典

根据 FirefoxDriver 类的 Selenium JavaDoc ,不再支持method为有效。
__FirefoxDriver(profile)``Constructor

相反,鼓励使用FirefoxOptions扩展的类,MutableCapabilities
org.openqa.selenium.MutableCapabilities

因此,当您通过执行每次创建新的 FirefoxProfiledriver = new FirefoxDriver(profile);,必须使用
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);
2020-06-26