title: Capture focused html element value from web page Opened in chrome browser
body: <p>I am working to capture web page control, web page is opened in chrome browser. </p>
  
  <p>I am using System.Windows.Automation.AutomationElement. My c# code is as below.</p>
  
  <pre><code>static void Main()            
  {
     Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
  }
  static void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args)
  {   
      AutomationElement element = src as AutomationElement;
      if (element != null)
      {
          string name = element.Current.Name;
          string value = element.GetText();       
          if (value != string.Empty)
          {
              using (Process process = Process.GetProcessById(processId))
              {
                  if (process.ProcessName != null &amp;&amp; process.ProcessName.Contains("chrome"))
                  {                   
                      Console.WriteLine("  Name: {0},Value: {1}", name, value);
                  }
              }
          }
      }
  }
  public static class AutomationExtensions
      {
          public static string GetText(this AutomationElement element)
          {
              object patternObj;
              if (element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
              {
                  var valuePattern = (ValuePattern)patternObj;
                  return valuePattern.Current.Value;
              }
              else if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
              {
                  var textPattern = (TextPattern)patternObj;
                  return textPattern.DocumentRange.GetText(-1).TrimEnd('\r'); // often there is an extra '\r' hanging off the end.
              }
              else
              {
                  return element.Current.Name;
              }
          }
      }
  </code></pre>
  
  <p>I got browser URL value when user change focus from address bar.</p>
  
  <p>I want to capture html element values.Right now I am getting following.</p>
  
  <p>"Name: Chrome Legacy Window,Value: Chrome Legacy Window "</p>
  
  <p>please help me.</p>
  
  <p>NOTE:
  Type "chrome://accessibility/" in address bar and set accessible to true for web site allows to capture values. </p>
  
  <p>I need to capture without setting accessibility attribute, as it is impossible to set flag.</p>