gpt4 book ai didi

c# - 更改每个文件的下载位置和名称

转载 作者:行者123 更新时间:2023-11-30 17:32:51 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

您可以将 MS UI 自动化与 TestStack.White 结合使用。这非常困难,但确实有效。

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();

关于c# - 更改每个文件的下载位置和名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com