如何使用Selenium和Firefox自动下载可执行文件

时间:2018-09-19 10:18:34

标签: c# selenium-webdriver selenium-firefoxdriver

当我单击Firefox中的链接时,我正在使用Selenium Webdriver和C#自动自动下载可执行文件。理想情况下,我也想执行该文件,但现在,我想下载该文件并稍后执行。这是我到目前为止的内容:

public async void StartDriverFireFox(string proxy)
{ 
    FirefoxDriverService chromeDriverService = FirefoxDriverService.CreateDefaultService();
    chromeDriverService.HideCommandPromptWindow = true;
    var downloadDirectory = @Application.StartupPath.ToString() + "\\indirilen\\";

    FirefoxOptions options = new FirefoxOptions();
    options.SetPreference("browser.download.folderList", 2);
    options.SetPreference("browser.download.manager.showWhenStarting", false);
    options.SetPreference("browser.download.dir", downloadDirectory);
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/plain");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-powerpoint");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/png");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/gif");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-rar-compressed");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
    options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    options.SetPreference("browser.helperApps.neverAsk.openFile", "");
    options.SetPreference("browser.helperApps.alwaysAsk.force", false);
    options.SetPreference("browser.download.manager.alertOnEXEOpen", false);
    options.SetPreference("browser.download.manager.focusWhenStarting", false);
    options.SetPreference("browser.download.manager.useWindow", false);
    options.SetPreference("browser.download.manager.showAlertOnComplete", false);
    options.SetPreference("browser.download.manager.closeWhenDone", true);
    options.SetPreference("dom.disable_beforeunload", true);
    options.SetPreference("pdfjs.disabled", true);
    twitter = new FirefoxDriver(chromeDriverService,options);
    await Task.Delay(2000);
}

我们可以完全关闭下载窗口吗?现在,下载窗口将显示在doc和docx文件中。 谢谢。

0 个答案:

没有答案