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

1 回答 1

2

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

相反,鼓励使用FirefoxOptions扩展的类,MutableCapabilitiesorg.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);    
于 2017-11-30T08:45:39.247 回答