从chrome到C#获取URL无效

时间:2014-10-15 11:33:36

标签: c# .net google-chrome

- 我找到了这个解决方案:

    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;
   }
  • 但是在最后的chrome版本(32.0.1700.102 m)中没有使用。有人可以告诉更常见的解决方案吗?
  • Chrome可能已经改变了一些东西,上面的步行需要修改。 :(
  • 提前致谢。

2 个答案:

答案 0 :(得分:2)

使用此代替: element.FindAll(TreeScope.Subtree,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Edit)); 这段代码有效:

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;
    }

}

}

答案 1 :(得分:0)

这是格式化的代码。 Avia,拥有最多' CURRENT'回答。就像我提到的那样,我不喜欢它,因为没有一些标准的'浏览器甚至各种版本的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;
}