2

I have an application that monitors the URL of a browser. Currently I'm using chrome. I followed the solution from this answer:

public static string GetChromeUrl(Process process) {
    if (process == null)
        throw new ArgumentNullException("process");
    if (process.MainWindowHandle == IntPtr.Zero)
        return null;
    AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
    if (element == null)
        return null;
    AutomationElement edit = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
    return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}

Then I have this method to log the URL in a text file (using log4net):

while (true) {
    foreach (Process process in Process.GetProcessesByName("chrome")) {
        string url = GetChromeUrl(process);
        if (url == null)
            continue;
        Console.WriteLine(url); //for viewing purpose, it actually logs in orig code
    }
    Thread.Sleep(1000);
}

It works pretty well. But somehow there's an inconsistency with the GetChromeUrl method. Sometimes it returns null, and this is my big problem. Does anyone has a better solution?

Community
  • 1
  • 1
Fadz
  • 84
  • 2
  • 10
  • What line returns null? the MainWindowHandle check? the FromHandle check, or the Current.Value? – Simon Mourier Jul 08 '13 at 13:39
  • Hi Simon, This lines Returns the null AutomationElement edit = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); I also noticed that when you right click on the page in chrome then do nothing, it will also returns a null value. Thanks – Fadz Jul 09 '13 at 02:32
  • And i found another way to throw a null. if you mouseover on a link or tittle bar, title bar with long text(if it popups the whole text or as tooltip pops) it will throws a null. looks like this will cause a problem in my work. – Fadz Jul 09 '13 at 03:48
  • Hmmm.. I don't have a magic bullet here. When you want to "program" a distant application that was not designed for this specifically, you only have building bricks (ui-automation) and you need to managed your way out with with tricks like in Patrick's answer. – Simon Mourier Jul 09 '13 at 20:25
  • Thanks for the reponse, i already created a solution, to store the previous link in a variable, and incase the user force it to throw nulls(by way of right clicking on the page, mouse over on a link or title bar and etc) since the code will throw an exception I've just catch it and retrieve the value of the previous link. – Fadz Jul 10 '13 at 02:42

1 Answers1

1

Which bit returns null? Windows will often overlook programs whose ShowInTaskbar property is changed. This could be the reason for a null in the first check (and second). Concerning the second fault I cannot provide any help but I had the same problem a few weeks ago. I decided to use the position element of Chrome's URL bar, change the cursor position to it, and then get the AutomationElement under the cursor:

    Form1_Load()
    {
        Cursor.Position = element's position;
        Timer.Start();
    }

    Timer_Tick()
    {
        AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));
            string current = element.Current.Name;
        Console.WriteLine(current); //log in text file...
    }

Does this provide an answer to your query?

Patrick Geyer
  • 1,515
  • 13
  • 30
  • Im trying your code, but i couldn't get the mouse.x and y? is this a value of mouse or the url bar? – Fadz Jul 09 '13 at 02:49
  • The mouse and the URL bar as we set the cursor to be at its position. Once you have the automationElement you can probably get an id and keep getting its name instead of having to have the mouse hovering there. I'd write it but I'm on my phone. Not worth an upvote at least? – Patrick Geyer Jul 09 '13 at 03:27
  • @Fadz You might be able to do new Point (Cursor.Position) but I'm not sure. – Patrick Geyer Jul 09 '13 at 03:28
  • @Fadz here are some links to getting the id of the URL bar: [AutomationID](http://msdn.microsoft.com/en-us/library/aa349646.aspx) and [MSDN:AutomationID](http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.runtimeidproperty.aspx). Have a crack at that method! – Patrick Geyer Jul 09 '13 at 04:27