我正在自动化一个安装程序,因为它没有(功能) /silent开关。它的工作基本上很好,但我遇到了一个似乎是路障-一个可怕的对话框。看起来像这。
我试过的是:
SendKey.Send/Wait(Enter/Escape)
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK__RETURN, 0);关键注入到过程中
injector.SimulateTap((int) centre.X + 150, (int) centre.Y + 75); 触摸注入,不注册在这个特定的窗口,但所有其他窗口工作良好。
Walking over the element as if it were just a normal element我几乎不知所措,因为我非常希望不用手动按这个按钮(这也会破坏其他的自动安装)。
我知道为什么找不到它(或者我认为是这样);因为它是一个新的对话,我认为enter不能工作,因为它被发送到错误的进程(对话框的父进程,安装程序)和我不知道的触摸注入。
文档没有讨论如何处理对话框(也都是C++)。
通常,我正在遍历元素,以找到继续安装的下一个控件。有几个控件:
string[] EGalaxTouchInstallSequence = {
@"Next >",
"I accept the terms of the license agreement",
@"Next >",
"None",
null, //this is supposed to represent a dialogue popup
"OK",
"Support Multi-Monitor System",
@"Next >",
@"Next >",
@"Next >",
@"Next >",
@"Next >"
};最初,我从窗口获取所有的句柄,如下所示:
AutomationElement UIElement = AutomationElement.FromHandle(SetupWindow);我重复了一遍列表:
foreach (string button in EGalaxTouchInstallSequence)
{
if (!FindAndClickElement(UIElement, button))
goto error;
}FindAndClickElement
private bool FindAndClickElement(AutomationElement UIElement, string ElementName)
{
AutomationElement element = null;
Condition ControlProperty = new PropertyCondition(AutomationElement.NameProperty, ElementName);
AutomationElementCollection elements = UIElement.FindAll(TreeScope.Descendants, ControlProperty);
foreach (AutomationElement e in elements)
{
if (e.Current.Name.Contains(ElementName))
{
Console.WriteLine("Match with: " + ElementName);
element = e;
}
}
//Click code is further down, irrelevant at this stage
}Element Exists
private bool ElementExists(AutomationElement UIElement, string ElementName)
{
Condition ControlProperty = new PropertyCondition(AutomationElement.NameProperty, ElementName);
AutomationElementCollection elements = UIElement.FindAll(TreeScope.Descendants, ControlProperty);
foreach (AutomationElement e in elements)
{
Console.WriteLine($"element name: {e.Current.Name}");
if (e.Current.Name.Contains(ElementName))
{
return true;
}
}
return false;
}发布于 2018-10-26 15:12:29
在你得到元素之前,我会检查一下里面有什么。我用这样的方法来得到所有的东西。
/// <summary>
/// Returns all children elements of an automation element.
/// </summary>
/// <param name="automationElement"></param>
/// <returns></returns>
public static List<AutomationElement> AllChildren(AutomationElement automationElement)
{
if (automationElement == null) throw new ArgumentNullException(nameof(automationElement));
AutomationElement firstChild = TreeWalker.RawViewWalker.GetFirstChild(automationElement);
if (firstChild == null)
return new List<AutomationElement>();
List<AutomationElement> children = new List<AutomationElement> { firstChild };
AutomationElement sibling;
int childIndex = 0;
while ((sibling = TreeWalker.RawViewWalker.GetNextSibling(children[childIndex])) != null)
{
children.Add(sibling);
childIndex++;
}
return children;
}此外,在我的评论中,请确保您不会意外地通过使用这个FindAll("...").FirstOrDefault(a => a.FirstChild().ClassName != "ToolTip");找到工具提示。工具提示通常不会显示,因为应用程序点击东西的速度非常快,所以它的位置将有一个(-1,-1)。
https://stackoverflow.com/questions/53010335
复制相似问题