Get Firefox URL with UI Automation
I am trying to get the url value in Firefox using the following code. The only problem is that it returns "Search or enter address" (see tree structure with Inspect.exe below). Looks like I need to go down one level. 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 a tree structure see:
source to share
It is not clear which element you are starting your search with, but you have two elements with that name. One is a combo box control, the other is an edit control. Try using 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 with a combo box, you can change TreeScope.Subtree
to instead TreeScope.Descendants
, since Subtree contains the current item in the search.
source to share