我希望能够遍历chrome页面上的所有选项卡并关闭任何youtube页面的选项卡.
我做了一些谷歌搜索并找到了下面的代码.有两个(可能更多)问题.首先,我创建了一个WPF应用程序并添加了System.Windows.Automation命名空间(使用visual studio 2015 .net 4.5),但无法识别AutomationElement.
此外,我不确定如何循环选项卡并测试页面是否是youtube页面.
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
return null;
foreach (Process proc in procsChrome)
{
// the chrome process must have a window
if (proc.MainWindowHandle == IntPtr.Zero)
continue;
// to find the tabs we first need to locate something reliable - the 'New Tab' button
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (SearchBar != null)
{
AutomationPattern[] patterns = SearchBar.GetSupportedPatterns();
if(patterns.Length > 0)
{
ValuePattern val = (ValuePattern)SearchBar.GetCachedPattern(patterns[0]);
if (val.Current.Value.Contains("youtube.com") || val.Current.Value.Contains("youtube.co.uk"))
proc.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
System.Windows.Automation位于UIAutomationClient.dll中.您是否添加了UIAutomationClient.dll作为项目的参考?
检查值"youtube".
if (SearchBar != null)
{
AutomationPattern[] patterns = SearchBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)SearchBar.GetCurrentPattern(patterns[0]);
if(val.Current.Value.Contains("youtube.com"))
proc.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
根据你的问题,我写了一个小程序来实现这一点。让我知道它是否有效。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main()
{
Process[] procs = Process.GetProcessesByName("chrome");
if (procs.Length == 0)
{
Console.WriteLine("Google Chrome is not currently open");
return;
}
List<string> titles = new List<string>();
IntPtr hWnd = IntPtr.Zero;
int id = 0;
int numTabs = procs.Length;
foreach (Process p in procs)
{
if (p.MainWindowTitle.Length > 0)
{
hWnd = p.MainWindowHandle;
id = p.Id;
break;
}
}
bool isMinimized = IsIconic(hWnd);
if (isMinimized)
{
ShowWindow(hWnd, 9); // restore
Thread.Sleep(100);
}
SetForegroundWindow(hWnd);
SendKeys.SendWait("^1"); // change focus to first tab
Thread.Sleep(100);
int next = 1;
string title;
while (next <= numTabs)
{
try
{
title = Process.GetProcessById(id).MainWindowTitle.Replace(" - Google Chrome", "");
if (title.ToLower().Contains("youtube"))
{
SendKeys.SendWait("^{w}"); // close tab.
Thread.Sleep(100);
}
next++;
SendKeys.SendWait("^{TAB}"); // change focus to next tab
Thread.Sleep(100);
}
catch (Exception ex)
{
// Chrome internal process, doesn't have tab.
}
}
if (isMinimized)
{
ShowWindow(hWnd, 6); // minimize again
Thread.Sleep(100);
}
hWnd = Process.GetCurrentProcess().MainWindowHandle;
SetForegroundWindow(hWnd);
Thread.Sleep(100);
Console.WriteLine("Closed youtube tabs");
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)