I know how to get all the names of the Chrome tabs and the URL of the current active tab in VB I want to get a unique ID for each tab, because there are tabs with the same name and URL
Code:
Public Function GetBrowserUrlActivaChrome() As string
Dim procsChrome As Process() = Process.GetProcessesByName("chrome")
For Each chrome As Process In procsChrome
If chrome.MainWindowHandle = IntPtr.Zero Then Continue For
Dim element As UIAutomationClient.CUIAutomation = New UIAutomationClient.CUIAutomation()
Dim root As UIAutomationClient.IUIAutomationElement = element.ElementFromHandle(chrome.MainWindowHandle)
If element Is Nothing Then Return ""
Dim condition1 As UIAutomationClient.IUIAutomationCondition = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ProcessIdPropertyId, chrome.Id)
Dim condition2 As UIAutomationClient.IUIAutomationCondition = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsControlElementPropertyId, True)
Dim condition3 As UIAutomationClient.IUIAutomationCondition = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsContentElementPropertyId, True)
Dim condition4 As UIAutomationClient.IUIAutomationCondition = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ControlTypePropertyId, UIAutomationClient.UIA_ControlTypeIds.UIA_EditControlTypeId)
Dim conditions As UIAutomationClient.IUIAutomationCondition() = New UIAutomationClient.IUIAutomationCondition(3) {}
conditions(0) = condition1
conditions(1) = condition2
conditions(2) = condition3
conditions(3) = condition4
Dim andCondition As UIAutomationClient.IUIAutomationCondition = element.CreateAndConditionFromArray(conditions)
Dim elementx As IUIAutomationElement = root.FindFirst(UIAutomationClient.TreeScope.TreeScope_Descendants, andCondition)
If elementx IsNot Nothing Then
return (CType(elementx.GetCurrentPattern(UIAutomationClient.UIA_PatternIds.UIA_ValuePatternId), UIAutomationClient.IUIAutomationValuePattern)).CurrentValue.ToString()
End If
Next
Return String.Empty
End Function
Public Sub AllTabsNames()
Dim procsChrome As Process() = Process.GetProcessesByName("chrome")
For Each chrome As Process In procsChrome
If chrome.MainWindowHandle = IntPtr.Zero Then Continue For
Dim winName As String = chrome.MainWindowTitle
Dim q As Integer = InStr(winName, " - Google Chrome")
If q > 0 Then winName = Mid(winName, 1, q - 1)
Dim root As AutomationElement = AutomationElement.FromHandle(chrome.MainWindowHandle)
Dim condNewTab As Condition = New PropertyCondition(AutomationElement.NameProperty, winName)
Dim elmNewTab As AutomationElement = root.FindFirst(TreeScope.Descendants, condNewTab)
Dim treewalker As TreeWalker = TreeWalker.ControlViewWalker
Dim elmTabStrip As AutomationElement = treewalker.GetParent(elmNewTab)
Dim condTabItem As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)
For Each tabitem As AutomationElement In elmTabStrip.FindAll(TreeScope.Children, condTabItem)
Debug.Print(tabitem.Current.Name )
Next
Next
end sub