【问题标题】:How to download and save excel and pdf file from pop-up in a desired location using Selenium written in C#如何使用用 C# 编写的 Selenium 从弹出窗口中下载并保存 excel 和 pdf 文件到所需位置
【发布时间】:2017-12-15 18:54:07
【问题描述】:

我想知道自动化测试是否有任何方法可以下载文件(在我的情况下为 excel 和 pdf)并使用 selenium Web 驱动程序保存在所需的位置。我尝试使用 Firefox 配置文件,但没有奏效。 测试运行时,会弹出窗口询问是否打开或保存文件。 所以,当我们点击一​​个按钮时,我不希望弹出窗口显示,而是自动允许它下载到所需的位置(本地和 Selenium 服务器上) 我们正在使用 C# 编写测试。附件是弹出窗口。 有人可以帮忙吗?

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

【问题讨论】:

  • 您是否尝试过选中“自动执行...”并将其保存为 Firefox 配置文件?还要确保不要修改在 Selenium 测试会话期间创建的临时配置文件!
  • 我该如何处理“自动执行此操作..”,因为它是一个自动化测试并且它是一个弹出窗口而不是浏览器?

标签: c# selenium browser-automation remotewebdriver firefox-profile


【解决方案1】:
  1. 加载您用于测试的 Firefox 配置文件,尝试下载任何 .xls 文件 - 您会看到此弹出窗口。
  2. 勾选复选框并下载您希望在自动测试中下载的文件。

下次您在自动测试中使用此配置文件打开 Firefox 时,将下载这些 xls 文件,而不会弹出任何窗口

【讨论】:

    【解决方案2】:

    您似乎忘记在 browser.helperApps.neverAsk.saveToDisk 首选项中包含一些 MIME 类型。

    以下是 .xls.xlsx.pdf 文件扩展名的 MIME 类型:

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

    参考资料:


    要隐藏 .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));
    

    【讨论】:

    • 谢谢。添加此 mime 类型在我的本地工作。但是,如果我使用 selnium 集线器节点在远程 Web 驱动程序中运行相同的测试,它不会下载文件。我已经在我的远程机器中创建了文件夹结构以便下载文件。那么,有什么想法让它在远程工作?
    • 嗯...不幸的是我不确定。我想它会一样工作,你确认文件夹结构与你的本地机器完全匹配吗?
    • 是的,我已经在控制台输出中验证了它,它返回相同的文件夹结构。
    • 那么不确定,我对远程网络驱动程序的工作不多,对此感到抱歉。
    猜你喜欢
    • 2017-12-19
    • 2013-05-20
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多