c# - 使用 UI 自动化获取 Firefox URL

标签 c# firefox ui-automation

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

最佳答案

不清楚您从哪个元素开始搜索,但您有两个具有该名称的元素。一个是组合框控件,另一个是编辑控件。尝试使用 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/

相关文章:

c# - 当被 JSON.NET 序列化时,属性的顺序变得困惑

linux - 如何通过命令行在 VPS 上安装 Firefox?

ios - 在许多 Mac 上运行 UIAutomation 脚本?

vba - VB : Assigning to a Boolean property in Adobe Illustrator, Photoshop

c# - 使用 Microsoft UI Automation 获取任何应用程序的 TitleBar Caption?

c# - 将泛型 ienumerable 转换为 arraylist

c# - DataGridTemplateColumn.HeaderTemplate 问题

c# - Visual Studio 2010 中的 GTK#

css - Mozilla Firefox userChrome.css?

javascript - WebGL 2.0 支持短 3D 纹理