使用UI自动化获取Firefox浏览器的URL

3

我正在尝试使用以下代码在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;
}

关于树形结构,请参见:

enter image description here

1个回答

4

目前不清楚你从哪个元素开始搜索,但是有两个名称相同的元素。一个是组合框控件,另一个是编辑控件。尝试使用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包括在搜索中的当前元素。

1
无法转换,出现错误的强制转换异常 online::---- ValuePatternval = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]); - Ankur Tripathi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接