简体   繁体   English

如何使用以C#编写的Selenium在所需位置从弹出窗口下载并保存Excel和pdf文件

[英]How to download and save excel and pdf file from pop-up in a desired location using Selenium written in C#

I would like to know if there is any way for the automated tests to download the file (excel and pdf in my case) and save in a desired location using selenium web driver. 我想知道是否有任何方法可以使自动化测试下载文件(在我的情况下为Excel和pdf)并使用Selenium Web驱动程序保存在所需的位置。 I tried using Firefox profile, but that didn't work. 我尝试使用Firefox配置文件,但这没有用。 When the test is running, there is window pop-up with asking whether to open or save the file. 测试运行时,会弹出一个窗口,询问您是否打开或保存文件。 So, when we click a button, I do not want the windows pop-up to display, instead automatically allow it to download in a desired location( both locally and on Selenium Server) We are using C# to write the tests. 因此,当我们单击一个按钮时,我不希望显示窗口弹出窗口,而是自动允许它在所需位置(本地和Selenium服务器上)下载。我们正在使用C#编写测试。 Attached is the windows pop-up. 附带的是窗口弹出窗口。 Could someone please help with this? 有人可以帮忙吗?

在此处输入图片说明

public static IWebDriver Build(SeleniumInstanceContext context)
    {
        IWebDriver instance;            
        var capabilities = new DesiredCapabilities();
        var profile = CreateFirefoxProfile();

        //Pass the Firefox profile to be used by RemoteWebDriver
        capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

        switch (context.TestingBrowser.ToUpperInvariant())
        {
            case "CHROME":
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), DesiredCapabilities.Chrome())
                    : new ChromeDriver();
                break;
            case "IE":
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl),
                        DesiredCapabilities.InternetExplorer())
                    : new InternetExplorerDriver();
                break;
            default:
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), capabilities,
                        TimeSpan.FromMinutes(5))
                    : new FirefoxDriver(profile);
                break;
        }

        return instance;
    }

[![enter image description here][1]][1]public static FirefoxProfile CreateFirefoxProfile()
    {
        //Create FireFox Profile object
        var profile = new FirefoxProfile();

        //Set Location to store files after downloading.
        const string path = "C:\\Users\\abc.xyz\\Downloads";
        profile.SetPreference("browser.download.dir", path);
        profile.SetPreference("browser.download.folderList", 2);

        //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
        profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/csv");
        //profile.SetPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");

        //profile.SetPreference("browser.download.manager.showWhenStarting", false);
        profile.SetPreference("pdfjs.disabled", true);

        profile.SetPreference("browser.download.alertOnEXEOpen", false);
        profile.SetPreference("browser.download.manager.focusWhenStarting", false);
        profile.SetPreference("browser.helperApps.alwaysAsk.force", true);
        profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.SetPreference("browser.download.manager.closeWhenDone", false);
        profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
        profile.SetPreference("browser.download.manager.useWindow", false);

        return profile;
    }
  1. Load the Firefox profile you use for testing, try to download any .xls file - you'll see this pop-up. 加载用于测试的Firefox配置文件,尝试下载任何.xls文件-您将看到此弹出窗口。
  2. Tick the checkbox and download file like you want it to be downloaded in your autotests. 勾选复选框并下载文件,就像您希望在自动测试中下载文件一样。

Next time you open Firefox with this profile in autotests these xls files will be downloaded without any pop-up window 下次当您使用此配置文件在自动测试中打开Firefox时,这些xls文件将被下载,没有任何弹出窗口

It appears that you've forgotten to include some MIME types in the browser.helperApps.neverAsk.saveToDisk preference. 看来您已经忘记在browser.helperApps.neverAsk.saveToDisk首选项中包含一些MIME类型。

Here are the MIME types for .xls , .xlsx , and .pdf file extensions: 这是.xls.xlsx.pdf文件扩展名的MIME类型:

  • .xls - application/excel .xls application/excel
  • .xls - application/vnd.ms-excel .xls - application/vnd.ms-excel
  • .xls - application/x-excel .xls application/x-excel
  • .xls - application/x-msexcel .xls application/x-msexcel
  • .xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • .pdf - application/pdf .pdf application/pdf

References: 参考文献:


To hide this pop up for .xls , .xlsx , and .pdf file extensions, add the MIME types, separated by a comma: 要为.xls.xlsx.pdf文件扩展名隐藏此弹出窗口,请添加MIME类型,并用逗号分隔:

string[] mimeTypes = new string[]
{
    // .xls
    "application/excel",
    "application/vnd.ms-excel",
    "application/x-excel",
    "application/x-msexcel",

    // .xlsx
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

    // .pdf
    "application/pdf"
};

FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
    string.Join(",", mimeTypes));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium C# 弹出窗口处理 - Selenium C# Pop-up handling 如何使用C#中的硒自动在webrtc中单击“允许/阻止”按钮以弹出麦克风权限? - How to automate the clicking of 'Allow/Block' button in webrtc for microphone permission pop-up using selenium in C#? 如何使用 C# 处理 Outlook 中的安全弹出窗口 - How to handle security pop-up in outlook using C# c# Selenium:无法切换到模态/弹出窗口/iFrame - c# Selenium: Unable to SwitchTo Modal/Pop-up/iFrame 我在C#中使用Selenium Webdriver,无法在证书弹出窗口中单击“确定” - I am using selenium webdriver in C#, I am unable to click ok in the certificate pop-up 如何使用Selenium C#从webapp下载生成的pdf文件并将其附加到Visual Studio中的测试结果 - How to download generated pdf file from webapp and attach it to test results in Visual Studio using Selenium C# 如何为用户提供将文件保存在 C# 中所需位置的选项? - How to give user an options to save file in there desired location in C#? 使用ASP.Net中的C#为弹出窗口输出CSV文件 - to output CSV file for pop-up window using C# in ASP.Net 如何处理 selenium c# 中弹出的 window 文件上传 - How to handle a window file upload pop up in selenium c# 在C#中创建弹出式表单 - Creating a Pop-Up form in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM