试图像这样获取网址,但是我遇到了“ System.InvalidOperationException”-UIAutomationClient.dll
foreach (Process process in Process.GetProcessesByName("chrome"))
{
if (process.MainWindowHandle == IntPtr.Zero)
{
continue;
}
// to find the tabs we first need to locate something reliable - the 'New Tab' button
AutomationElement root = AutomationElement.FromHandle(process.MainWindowHandle);
Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "Новая вкладка");
AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
// get the tabstrip by getting the parent of the 'new tab' button
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
// loop through all the tabs and get the names which is the page title
Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
Console.WriteLine(elmTabStrip.FindAll(TreeScope.Children, condTabItem).Count);
foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
{
string url = ((ValuePattern)tabitem.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
Console.WriteLine(url);
}