我的问题已经解决了
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Automation;
namespace ui_automation
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
foreach (Process process in Process.GetProcessesByName("chrome"))
{
string url = GetChromeUrl(process);
if (url == null)
continue;
MessageBox.Show(url);
}
}
public static string GetChromeUrl(Process process)
{
string out_url = null;
if (process == null) {
out_url = null;
} else if (process.MainWindowHandle == IntPtr.Zero) {
out_url = null;
} else {
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.ProcessIdProperty, process.Id),
new PropertyCondition(AutomationElement.IsControlElementProperty, true),
new PropertyCondition(AutomationElement.IsContentElementProperty, true),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)
);
AutomationElement elementx = element.FindFirst(TreeScope.Descendants, conditions);
out_url = ((ValuePattern)elementx.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
return out_url;
}
}
}
但这不是我想要的。这段代码有效,但它仍然从 chrome 获取 URL 到慢... 2 秒甚至 3 有时。
我注意到,当我将 TreeScope.Descendant 更改为 TreeScope.Children 时,此代码开始运行 lika a flash :) 但返回 null - 没有找到。
有任何想法吗 ?