更改每个文件的下载位置和名称 [英] Change download location and name for each file

查看:118
本文介绍了更改每个文件的下载位置和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium和Chrome WebDriver进行自动化.该应用程序必须进行一系列下载,需要使用不同的名称(数据+报表类型")和与我正在下载的报表类型相匹配的文件夹进行保存.

I'm doing an automation using Selenium with Chrome WebDriver. The application has to do a series of downloads that need to be saved with different names (Data + Report type) and folders that match the type of report I'm downloading.

问题是我只能在实例化新驱动程序时设置默认目录

The problem is that I can only set the default directory when I instantiate a new driver

var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

IWebDriver driver = new ChromeDriver(@"location chromeDriver", chromeOptions);

driver.Navigate().GoToUrl(url);

因此,我无法重命名文件名或选择相应的目录. 有谁知道我该怎么做?

Therefore, I can not rename the file name or select the corresponding directory. Does anyone have any idea how I can do this?

推荐答案

您可以将MS UI Automation与TestStack.White一起使用.这很困难,但是可以肯定.

You can use MS UI Automation with TestStack.White. It's quite difficult but it works for sure.

using System.Text.RegularExpressions;
using System.Windows.Automation;
using TestStack.White.InputDevices;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
...
public class SaveAsWindow
{
    AutomationElement _dialog;
    Window _win;

    public SaveAsWindow(string title)
    {
        List<Window> winList = TestStack.White.Desktop.Instance.Windows();
        foreach (Window win in winList)
        {
            Regex r = new Regex(title, RegexOptions.IgnoreCase);
            Match m = r.Match(win.Title);
            if (m.Success)
            {
                _win = win;
            }
        }
        _dialog = _win.GetElement(SearchCriteria.ByControlType(ControlType.Window));
    }

    public void Close()
    {

        Condition condition = new PropertyCondition(AutomationElement.NameProperty, "Cancel");
        AutomationElement noButton = _dialog.FindFirst(TreeScope.Children, condition);

        System.Windows.Point p = noButton.GetClickablePoint();
        Mouse.Instance.Click(p);
    }

    public string FileName
    {
        set
        {
            TextBox fileName =_win.Get<TextBox>(SearchCriteria.ByAutomationId("1001"));
            fileName.Text = value;
        }
    }

    public void Save()
    {
        Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "1");
        AutomationElement saveButton = _dialog.FindFirst(TreeScope.Children, condition);

        System.Windows.Point p = saveButton.GetClickablePoint();
        Mouse.Instance.Click(p);
        System.Threading.Thread.Sleep(1000);
    }
}

//// Usage
IWebDriver driver = new ChromeDriver(@"location chromeDriver", chromeOptions);
driver.Navigate().GoToUrl(url);
// it did something and save as window appears.
var saveWindow = new SaveAsWindow("title of Chrome browser");
saveWindow.FileName = "c:\what-ever.xlsx";
saveWindow.Save();

这篇关于更改每个文件的下载位置和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆