我正在尝试使用以下代码在 Firefox 中获取 URL 的值。问题是它只返回“搜索或输入地址”(请参阅下面的 Inspect.exe 树结构)。看来我需要降一级。谁能告诉我如何做到这一点。
public static string GetFirefoxUrl(IntPtr pointer) {
AutomationElement element = AutomationElement.FromHandle(pointer);
if (element == null)
return null;
AutomationElement tsbCtrl = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Search or enter address"));
return ((ValuePattern)tsbCtrl.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
树状结构见:
最佳答案
不清楚您从哪个元素开始搜索,但您有两个具有该名称的元素。一个是组合框控件,另一个是编辑控件。尝试使用 AndCondition
组合多个 PropertyCondition 对象:
var nameCondition = new PropertyCondition(AutomationElement.NameProperty, "Search or enter address");
var controlCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
var condition = new AndCondition(nameCondition, controlCondition);
AutomationElement editBox = element.FindFirst(TreeScope.Subtree, condition);
// use ValuePattern to get the value
如果搜索从组合框开始,您可以改为将 TreeScope.Subtree
更改为 TreeScope.Descendants
,因为 Subtree 包括搜索中的当前元素。
关于c# - 使用 UI 自动化获取 Firefox URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26348382/