Tawee Taenkom
Tawee Taenkom

Reputation: 31

Get URL from chrome to C# not working

-I found this solution:

    static void Main(string[] args)
    {
        foreach (Process process in Process.GetProcessesByName("chrome"))
        {
            string url = GetChromeUrl(process);
            if (url == null)
                continue;

            Console.WriteLine("CH Url for '" + process.MainWindowTitle + "' is " + url);
        }
    }
   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;
   }

Upvotes: 1

Views: 3056

Answers (2)

Avia
Avia

Reputation: 1829

Use this instead: element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); this code works :

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;

namespace tests
{
    class Program
    {
     static void Main(string[] args)
     {
        foreach (Process process in Process.GetProcessesByName("chrome"))
        {
            string url = GetChromeUrl(process);
            if (url == null)
                continue;

            Console.WriteLine("CH Url for '" + process.MainWindowTitle + "' is " + url);
        }
    }
    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;

        AutomationElementCollection edits5 = element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
        AutomationElement edit = edits5[0];
        string vp = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
        Console.WriteLine(vp);
        return vp;
    }

}

}

Upvotes: 2

Iunknown
Iunknown

Reputation: 367

Here's formatted code. Avia, has the most 'CURRENT' answer. Like I mentioned, I don't like it since there isn't some 'standard' way across browsers or even within various versions of Chrome.

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));
  if (edit == null)
  {
    AutomationElementCollection edits5 = element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
    edit = edits5[0];
  }
  return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}

Upvotes: 0

Related Questions