0

Hi everyone I need to get the url from a process, I'have made this

var Element = AutomationElement.FromHandle(Process.MainWindowHandle);
            AutomationElement edit = Elemento.FindFirst(TreeScope.Subtree,
                         new AndCondition(
                              new PropertyCondition(AutomationElement.NameProperty, "Barra de direcciones y de búsqueda ", PropertyConditionFlags.IgnoreCase),
                              new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
            if (edit is null)
            {
                return "Null";
            }
            return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value;

this code works fine on my computer (Windows 11) but when I run this code on another PC it doesn't work, sometimes it crashes,it doesn't found the search bar or sometimes found another AutomationElement like the search bar from youtube, I need your help, How can I make this code work correctly on diferent computers?

  • Did you check the solutions from other questions like this one : https://stackoverflow.com/questions/593709/how-to-get-the-url-of-the-current-page-in-c-sharp – Darkk L Apr 20 '22 at 15:50
  • 1
    If with *web explorer* you mean Edge Chrome, use the `ClassName` Property instead of the localized `Name` Property. The `ClassName` is `OmniboxViewViews`. The `AndCondition` is not needed anymore (it's the only Edit element with that class name). – Jimi Apr 20 '22 at 15:58
  • 1
    If it's not Edge, well, do the same thing anyway (i.e., avoid localizable property values as reference). – Jimi Apr 20 '22 at 16:13
  • @Jimi Sorry I didn't explain which web explorer, I need it for Microsoft Edge, Chrome and Mozilla Firefox and I just saw that Chrome and Firefox don't have class name – Luis Garcia Apr 20 '22 at 16:15
  • 1
    The search bar of FireFox doesn't have a `ClassName` property, but it's the only Edit element type that is descendant of the the main Window (the only Edit element descendant of a ToolBar element in any case) – Jimi Apr 20 '22 at 16:44
  • 1
    In other words, you don't necessarily need an `AndCondition` or an `OrCondition` to find an element, you can use just one Property (a single `Condition`). – Jimi Apr 20 '22 at 16:55

0 Answers0