C# 如何在Web UI自动化中处理Windows对话框?
我遇到了以下问题。我使用WatiN和C#来自动化Web UI应用程序的场景 在网页上,当我单击“打印”按钮时,会打开一个对话框窗口“将文件另存为”,我需要在其中输入路径并单击对话框中的“保存”按钮。单击“打印”按钮后,浏览器将继续加载,直到我输入路径并通过对话框保存 我目前所做的:C# 如何在Web UI自动化中处理Windows对话框?,c#,watin,ui-automation,C#,Watin,Ui Automation,我遇到了以下问题。我使用WatiN和C#来自动化Web UI应用程序的场景 在网页上,当我单击“打印”按钮时,会打开一个对话框窗口“将文件另存为”,我需要在其中输入路径并单击对话框中的“保存”按钮。单击“打印”按钮后,浏览器将继续加载,直到我输入路径并通过对话框保存 我目前所做的: 使用WatiN,我尝试使用WatiN.Core.Native.Windows类来获取子窗口(将文件另存为对话框)-父窗口将是浏览器。当我尝试使用它时,我发现没有选项可以找到编辑文本框来发送路径 System.Int
System.IntPtr ptr = Utility.Browser.hWnd;
WatiN.Core.Native.Windows.Window mainWin = new WatiN.Core.Native.Windows.Window(ptr);
System.IntPtr childptr = WatiN.Core.Native.Windows.NativeMethods.GetChildWindowHwnd(mainWin.Hwnd, "#32770"); // #32770 is the class name , retrieved from Inspect.exe.
但我总是将childptr设置为“0”WatiN.Core.Native.Windows.Window windowMain = new WatiN.Core.Native.Windows.Window(NativeMethods.GetWindow(ptr, 5)); // ptr as declared and initialized above
TreeWalker trw = new TreeWalker(Condition.TrueCondition);
AutomationElement mainWindow = trw.GetParent(AutomationElement.FromHandle(ptr));
但我在最后一行被卡住了。我怀疑,这可能是因为浏览器正在后台加载System.Windows.Forms.SendKeys.SendWait("path");
// Gets stuck in above line after entering the path. Tried with only Send method, it says this method can not be used for this type of application.
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
谢谢。我不熟悉watin,但我熟悉使用自动化元素的C#ui自动化。通过这个例子,我可以从谷歌浏览器上获得保存对话框
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
AutomationElement aeDesktop = AutomationLibrary.GetDesktop();
AutomationElement aeChrome =
AutomationLibrary.GetAutomationElement(
"Gmail - Verify your Disqus.com account - Google Chrome", aeDesktop);
List<AutomationElement> aeCollection = AutomationLibrary.GetAllAutomationElements(aeChrome);
foreach (AutomationElement ae in aeCollection)
{
System.Diagnostics.Debug.WriteLine(ae.Current.Name);
if (ae.Current.Name == "Save As")
{
List<AutomationElement> aeCollection2 = AutomationLibrary.GetAllAutomationElements(ae);
foreach (AutomationElement ae2 in aeCollection2)
{
System.Diagnostics.Debug.WriteLine(ae2.Current.Name);
}
return;
}
}
}
catch (UnableToFindAutomationElementException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private void按钮\u单击(对象发送者,路由目标)
{
尝试
{
AutomationElement aeDesktop=AutomationLibrary.GetDesktop();
自动元素aeChrome=
AutomationLibrary.GetAutomationElement(
“Gmail-验证您的discus.com帐户-谷歌浏览器”,aeDesktop);
List aeCollection=AutomationLibrary.GetAllAutomationElements(aeChrome);
foreach(自动采集中的元素ae)
{
System.Diagnostics.Debug.WriteLine(ae.Current.Name);
如果(ae.Current.Name==“另存为”)
{
List aeCollection2=自动图书馆.GetAllAutomationElements(ae);
foreach(aeCollection2中的AutomationElement ae2)
{
System.Diagnostics.Debug.WriteLine(ae2.Current.Name);
}
返回;
}
}
}
捕获(无法找到AutomationElementException ex)
{
系统.诊断.调试.写入线(例如消息);
}
}
GetAutomationElement和GetAllAutomationElements是我写的用来包装所有需要使用uiautomation进行的树遍历的东西。下面是GetAllAutomationeElement代码,如果您需要帮助,它应该很容易用于获取单个元素。我花了一点时间才弄明白如何取回所有这些
/// <summary>
/// Returns all children elements of an automation element.
/// </summary>
/// <param name="aeElement"></param>
/// <returns></returns>
public List<AutomationElement> GetAllAutomationElements(AutomationElement aeElement)
{
AutomationElement aeFirstChild = TreeWalker.RawViewWalker.GetFirstChild(aeElement);
List<AutomationElement> aeList = new List<AutomationElement>();
aeList.Add(aeFirstChild);
AutomationElement aeSibling = null;
int count = 0;
while ((aeSibling = TreeWalker.RawViewWalker.GetNextSibling(aeList[count])) != null)
{
aeList.Add(aeSibling);
count++;
}
return aeList;
}
//
///返回自动化元素的所有子元素。
///
///
///
公共列表GetAllAutomationElement(AutomationElement aeElement)
{
AutomationElement aeFirstChild=TreeWalker.RawViewWalker.GetFirstChild(aeElement);
List aeList=新列表();
aeList.Add(aeFirstChild);
AutomationElement可视性=空;
整数计数=0;
while((aeSibling=TreeWalker.RawViewWalker.GetNextSibling(aeList[count]))!=null)
{
aeList.Add(视觉化);
计数++;
}
返回aeList;
}
谢谢Max。我也尝试过使用UI自动化(在我的帖子中是第二个分辨率)。我面临的问题是,当浏览器仍在加载并显示“另存为”弹出窗口时,获取浏览器窗口指针及其子项(另存为窗口对话框)非常困难:(而且我无法找到解决方案。无论如何,如果浏览器未加载(在另一种情况下),它工作正常。)。