我在google上搜索与此问题标题相同的关键字。 有些代码无法工作或复杂的解释,即使在stackoverflow链接中也是如此。 我找到了一个链接工作here。 但该代码结果是页面标题,而不是网址。 如何在下面创建此代码以检索chrome上的当前网址活动标签? 这里是代码,
'//Grab all the Chrome processes
Dim chrome() As Process = Process.GetProcessesByName("chrome")
'//Exit if chrome isn't running
If chrome.Length <= 0 Then Exit Sub
For Each chromeProcess As Process In chrome
'//If the chrome process doesn't have a window handle then ignore it
If chromeProcess.MainWindowHandle <> IntPtr.Zero Then
'//To find the tabs we first need to locate something reliable - the 'New Tab' button
Dim rootElement As AutomationElement = AutomationElement.FromHandle(chromeProcess.MainWindowHandle)
Dim condNewTab As Condition = New PropertyCondition(AutomationElement.NameProperty, "New Tab")
Dim elemNewTab As AutomationElement = rootElement.FindFirst(TreeScope.Descendants, condNewTab)
'//Get the tabstrip by getting the parent of the 'new tab' button
Dim tWalker As TreeWalker = TreeWalker.ControlViewWalker
Dim elemTabStrip As AutomationElement = tWalker.GetParent(elemNewTab)
'//Loop through all the tabs and get the names which is the page title
Dim tabItemCondition As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)
For Each tabItem As AutomationElement In elemTabStrip.FindAll(TreeScope.Children, tabItemCondition)
Debug.WriteLine(tabItem.Current.Name)
Next
End If
Next
答案 0 :(得分:1)
解决,来自其他论坛,
Imports System.Windows.Automation
Module GoogleChrome
Private Const ChromeProcess As [String] = "chrome"
Private Const AddressCtl As [String] = "Address and search bar"
Public Function GetChromeActiveWindowUrl() As [String]
Dim procs = Process.GetProcessesByName(ChromeProcess)
If (procs.Length = 0) Then
Return [String].Empty
End If
Return procs _
.Where(Function(p) p.MainWindowHandle <> IntPtr.Zero) _
.Select(Function(s) GetUrlControl(s)) _
.Where(Function(p) p IsNot Nothing) _
.Select(Function(s) GetValuePattern(s)) _
.Where(Function(p) p.Item2.Length > 0) _
.Select(Function(s) GetValuePatternUrl(s)) _
.FirstOrDefault
End Function
Private Function GetUrlControl( _
proses As Process) _
As AutomationElement
Dim propCondition = _
New PropertyCondition( _
AutomationElement.NameProperty, _
AddressCtl)
Return AutomationElement _
.FromHandle(proses.MainWindowHandle) _
.FindFirst( _
TreeScope.Descendants, _
propCondition)
End Function
Private Function GetValuePatternUrl( _
element As Tuple(Of _
AutomationElement, AutomationPattern())) As [String]
Dim ap = element.Item2(0)
Dim ovp = element.Item1.GetCurrentPattern(ap)
Dim vp = CType(ovp, ValuePattern)
Return vp.Current.Value
End Function
Private Function GetValuePattern( _
element As AutomationElement) _
As Tuple(Of _
AutomationElement, _
AutomationPattern())
Return New Tuple(Of _
AutomationElement, _
AutomationPattern())( _
element, _
element.GetSupportedPatterns())
End Function
End Module
答案 1 :(得分:1)
如果您使用的是 .Net framework 2.0 ,则适用于Google Chrome 55和Firefox。将进程名称发送到方法(“CHROME”或“FIREFOX”)。此外,您必须导入UIAutomationClient.dll。我希望它有所帮助。
private string GetBrowserUrlActivaChromeFirefox(string process)
{
Process[] procsChrome = Process.GetProcessesByName(process);
foreach (Process chrome in procsChrome)
{
if (chrome.MainWindowHandle == IntPtr.Zero)
continue;
UIAutomationClient.CUIAutomation element = new UIAutomationClient.CUIAutomation();
UIAutomationClient.IUIAutomationElement root = element.ElementFromHandle(chrome.MainWindowHandle);
if (element == null)
return "";
UIAutomationClient.IUIAutomationCondition condition1 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ProcessIdPropertyId, chrome.Id);
UIAutomationClient.IUIAutomationCondition condition2 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsControlElementPropertyId, true);
UIAutomationClient.IUIAutomationCondition condition3 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsContentElementPropertyId, true);
UIAutomationClient.IUIAutomationCondition condition4 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ControlTypePropertyId, UIAutomationClient.UIA_ControlTypeIds.UIA_EditControlTypeId);
UIAutomationClient.IUIAutomationCondition[] conditions = new UIAutomationClient.IUIAutomationCondition[4];
conditions[0] = condition1;
conditions[1] = condition2;
conditions[2] = condition3;
conditions[3] = condition4;
UIAutomationClient.IUIAutomationCondition andCondition = element.CreateAndConditionFromArray(conditions);
UIAutomationClient.IUIAutomationElement elementx = root.FindFirst(UIAutomationClient.TreeScope.TreeScope_Descendants, andCondition);
return ((UIAutomationClient.IUIAutomationValuePattern)elementx.GetCurrentPattern(UIAutomationClient.UIA_PatternIds.UIA_ValuePatternId)).CurrentValue.ToString();
}
return string.Empty;
}
答案 2 :(得分:0)
此C#代码可在非英语浏览器上使用:
// Check for Chrome
if (processName.Equals("chrome"))
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero)
continue;
try
{
CUIAutomation _automation = new CUIAutomation();
var appElement = _automation.ElementFromHandle(chrome.MainWindowHandle);
IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[3];
conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ProcessIdPropertyId, chrome.Id);
conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Google Chrome");
conditionArray[2] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_PaneControlTypeId);
IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray);
IUIAutomationElement frameElement = appElement.FindFirst(TreeScope.TreeScope_Subtree, conditions);
if (frameElement != null)
{
conditionArray = new IUIAutomationCondition[2];
conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ProcessIdPropertyId, chrome.Id);
conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_EditControlTypeId);
conditions = _automation.CreateAndConditionFromArray(conditionArray);
frameElement = frameElement.FindFirst(TreeScope.TreeScope_Descendants, conditions);
if (frameElement != null)
return ((IUIAutomationValuePattern)frameElement.GetCurrentPattern(10002)).CurrentValue;
}
}
catch (Exception e)
{
continue;
}
}
}
COM界面比System.Windows.Automation中的旧界面效率更高。