使用UI自动化获取Firefox URL [英] Get Firefox URL with UI Automation

查看:409
本文介绍了使用UI自动化获取Firefox URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的代码来获取Firefox中URL的值。问题是它只返回搜索或输入地址(参见下面的Inspect.exe的树结构)。看起来我需要去一个层次。

  public static string GetFirefoxUrl(IntPtr pointer){
AutomationElement element = AutomationElement .FromHandle(指针);
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;

$ / code>

对于树结构,请参阅:

解决方案

目前还不清楚从哪个元素开始搜索,但是您有两个具有该名称的元素。一个是组合框控件,另一个是编辑控件。尝试使用 AndCondition 来组合多个PropertyCondition对象:

  var nameCondition = new PropertyCondition(AutomationElement.NameProperty,搜索或输入地址); 
var controlCondition = new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Edit);
var condition = new AndCondition(nameCondition,controlCondition);
AutomationElement editBox = element.FindFirst(TreeScope.Subtree,condition);
//使用ValuePattern获取值

如果搜索从组合框开始,你可以改变 TreeScope.Subtree TreeScope.Descendants ,因为Subtree在搜索中包含当前元素。


I am trying to get the value of the URL in Firefox using the following code. The problem is it only returns "Search or enter address" (see tree structure with Inspect.exe below). It looks like I need to go one level down. Can someone show me how to do this.

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

For the tree structure, see:

解决方案

It's not clear which element you are starting the search from, but you've got two elements with that name. One is a combo box control the other is an edit control. Try using using an AndCondition to combine multiple PropertyCondition objects:

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

If the search starts from the combo box, you could instead change TreeScope.Subtree to TreeScope.Descendants since Subtree includes the current element in the search.

这篇关于使用UI自动化获取Firefox URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆