Jump to content

How to activate specific tabs in Google Chrome?


xuzo
 Share

Recommended Posts

Haven't used AutoIt in years, so very rusty...

I can make autoIT activate windows of different programs, but having a hard time getting it to activate different tabs on Google chrome.

It only works if the window is open and all other tabs are closed.

#include <Constants.au3>
#include <AutoItConstants.au3>
Opt("WinTitleMatchMode", 2)


WinActivate ( "Calc" )
WinWait("Calc")
MouseClick($MOUSE_CLICK_LEFT, 100, 200, 2)
 send("{shiftdown}{end}{shiftup}{ctrlDown}c{ctrlUp}")
$title = ClipGet()
 ;send($title)

;this is the section where I would like a specific tab to be activated
WinActivate ( "Create +Prospects | Simple CRM - Cent Browser" )
WinWait("Create +Prospects | Simple CRM - Cent Browser")

;Opt("WinTitleMatchMode", 1)
;Local $hWnd= WinWait("Simple")
;WinActivate($hWnd)
MouseClick($MOUSE_CLICK_LEFT, 300, 500, 2)
Send($title)

 

Link to comment
Share on other sites

With information from UIASpy (Copy structure to clipboard) it's not that hard:

0000    Pane: AutoIt Scripting Language - AutoIt - Google Chrome
0001        Pane: Chrome Legacy Window
0002        TitleBar
0003            MenuBar: System
0004                MenuItem: System
0005            Button: Minimize
0006            Button: Maximize
0007            Button: Close
0008        Pane: Google Chrome
0009            Pane
0010                Button: Minimize
0011                Button: Maximize
0012                Button: Restore
0013                Button: Close
0014            Pane
0015                Pane
0016                    Document
0017                Pane
0018                    Tab
0019                        TabItem: AutoIt Scripting Language - AutoIt
0020                            Text: AutoIt Scripting Language - AutoIt
0021                            Pane
0022                            Button: Close
0023                        TabItem: Forums - AutoIt Forums
0024                            Text: Forums - AutoIt Forums
0025                            Pane
0026                            Button: Close
0027                        TabItem: AutoIt Help and Support - AutoIt Forums
0028                            Text: AutoIt Help and Support - AutoIt Forums
0029                            Pane
0030                            Button: Close
0031                        TabItem: How to activate specific tabs in Google Chrome? - AutoIt General Help and Support - AutoIt Forums
0032                            Text: How to activate specific tabs in Google Chrome? - AutoIt General Help and Support - AutoIt Forums
0033                            Pane
0034                            Button: Close
0035                        TabItem: AutoIt Example Scripts - AutoIt Forums
0036                            Text: AutoIt Example Scripts - AutoIt Forums
0037                            Pane
0038                            Button: Close
0039                        TabItem: UIASpy - UI Automation Spy Tool - AutoIt Example Scripts - AutoIt Forums
0040                            Text: UIASpy - UI Automation Spy Tool - AutoIt Example Scripts - AutoIt Forums
0041                            Pane
0042                            Button: Close
0043                        Button: New Tab
0044                    Pane
0045                        Button: Back
0046                        Button: Forward
0047                        Button: Reload
0048                        Custom
0049                            MenuItem: View site information
0050                            Edit: Address and search bar
0051                            Pane
0052                            Button: Bookmark this page
0053                            Pane
0054                        Custom: Extensions
0055                            Button: Blank New Tab Page
0056                            Separator
0057                            Separator
0058                        Button: Current user
0059                        MenuItem: Chrome
0060                Pane

Because this is about the outer elements of Chrome it's important that accessibility is not enabled.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#AutoIt3Wrapper_UseX64=y

#include "CUIAutomation2.au3"

Opt( "MustDeclareVars", 1 )

Example()

Func Example()
  ; Window handle
  Local $hWindow = WinGetHandle( "[CLASS:Chrome_WidgetWin_1]" )
  If Not IsHWnd( $hWindow ) Then Return ConsoleWrite( "$hWindow ERR" & @CRLF )
  ConsoleWrite( "$hWindow OK" & @CRLF )

  ; Activate window
  WinActivate( $hWindow )
  Sleep( 100 )

  ; UI Automation object
  Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation )
  If Not IsObj( $oUIAutomation ) Then Return ConsoleWrite( "$oUIAutomation ERR" & @CRLF )
  ConsoleWrite( "$oUIAutomation OK" & @CRLF )

  ; Desktop element
  Local $pDesktop, $oDesktop
  $oUIAutomation.GetRootElement( $pDesktop )
  $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF )
  ConsoleWrite( "$oDesktop OK" & @CRLF )

  ; Chrome window
  Local $pCondition
  $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Chrome_WidgetWin_1", $pCondition )
  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )

  Local $pChrome, $oChrome
  $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition, $pChrome )
  $oChrome = ObjCreateInterface( $pChrome, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oChrome ) Then Return ConsoleWrite( "$oChrome ERR" & @CRLF )
  ConsoleWrite( "$oChrome OK" & @CRLF )

  ; --- Forums - AutoIt Forums ---

  ; Tab item
  Local $pCondition1
  $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_TabItemControlTypeId, $pCondition1 )
  If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF )
  ConsoleWrite( "$pCondition1 OK" & @CRLF )

  ; Tab name
  Local $pCondition2 ; Note that $UIA_NamePropertyId ia a CASE SENSITIVE condition
  $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "Forums - AutoIt Forums", $pCondition2 )
  If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF )
  ConsoleWrite( "$pCondition2 OK" & @CRLF )

  ; And condition
  $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition )
  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )

  ; Find tab item
  Local $pTab, $oTab
  $oChrome.FindFirst( $TreeScope_Descendants, $pCondition, $pTab )
  $oTab = ObjCreateInterface( $pTab, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oTab ) Then Return ConsoleWrite( "$oTab ERR" & @CRLF )
  ConsoleWrite( "$oTab OK" & @CRLF )

  ; Rectangle
  Local $aRect ; l, t, w, h
  $oTab.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $aRect )
  If Not IsArray( $aRect ) Then Return ConsoleWrite( "$aRect ERR" & @CRLF )
  ConsoleWrite( "$aRect OK" & @CRLF )

  ; Activate tab
  MouseClick( "primary", $aRect[0]+$aRect[2]/2, $aRect[1]+$aRect[3]/2, 1, 0 )
EndFunc

 

Link to comment
Share on other sites

  • 2 years later...

@LarsJ and @junkew, amazing work on providing the numerous write ups on using UI Automation to get us over the hurdles of not being able to work with certain app technologies! I've spent the weekend soaking up as much of your examples and info as I can understand and want to chip in some stuff I figured out that helps me do things more natively. I'll sprinkle in the others nearer to where they are the topic of discussion.

LarsJ's answer above uses MouseClick in order to change to a targeted tab. That works just fine and taught me another helpful concept about UIA. Then I found a StackOverflow answer for a different language that had the magic sauce to be able to do it without a MouseClick. I've ported it to AutoIt here showing what to replace:

 

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#AutoIt3Wrapper_UseX64=y

;~ #include "CUIAutomation2.au3"
#include ".\Includes\UIA_Constants.au3" ;<--More recent approach (February 9, 2020 by LarsJ) https://www.autoitscript.com/forum/topic/201683-ui-automation-udfs/ 

Opt( "MustDeclareVars", 1 )

Example()

Func Example()
  ; Window handle
  Local $hWindow = WinGetHandle( "[CLASS:Chrome_WidgetWin_1]" )
  If Not IsHWnd( $hWindow ) Then Return ConsoleWrite( "$hWindow ERR" & @CRLF )
  ConsoleWrite( "$hWindow OK" & @CRLF )

  ; Activate window
  WinActivate( $hWindow )
  Sleep( 100 )

  ; UI Automation object
  Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtag_IUIAutomation ) ;<--Find & Replace All "$dtagIUIAutomation" with "$dtag_IUIAutomation" (underscore difference)
  If Not IsObj( $oUIAutomation ) Then Return ConsoleWrite( "$oUIAutomation ERR" & @CRLF )
  ConsoleWrite( "$oUIAutomation OK" & @CRLF )

  ; Desktop element
  Local $pDesktop, $oDesktop
  $oUIAutomation.GetRootElement( $pDesktop )
  $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
  If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF )
  ConsoleWrite( "$oDesktop OK" & @CRLF )

  ; Chrome window
  Local $pCondition
  $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Chrome_WidgetWin_1", $pCondition )
  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )

  Local $pChrome, $oChrome
  $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition, $pChrome )
  $oChrome = ObjCreateInterface( $pChrome, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
  If Not IsObj( $oChrome ) Then Return ConsoleWrite( "$oChrome ERR" & @CRLF )
  ConsoleWrite( "$oChrome OK" & @CRLF )

  ; --- Forums - AutoIt Forums ---

  ; Tab item
  Local $pCondition1
  $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_TabItemControlTypeId, $pCondition1 )
  If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF )
  ConsoleWrite( "$pCondition1 OK" & @CRLF )

  ; Tab name
  Local $pCondition2 ; Note that $UIA_NamePropertyId ia a CASE SENSITIVE condition
  $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "Forums - AutoIt Forums", $pCondition2 )
  If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF )
  ConsoleWrite( "$pCondition2 OK" & @CRLF )

  ; And condition
  $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition )
  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )

  ; Find tab item
  Local $pTab, $oTab
  $oChrome.FindFirst( $TreeScope_Descendants, $pCondition, $pTab )
  $oTab = ObjCreateInterface( $pTab, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
  If Not IsObj( $oTab ) Then Return ConsoleWrite( "$oTab ERR" & @CRLF )
  ConsoleWrite( "$oTab OK" & @CRLF )

;~   ; Rectangle
;~   Local $aRect ; l, t, w, h
;~   $oTab.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $aRect )
;~   If Not IsArray( $aRect ) Then Return ConsoleWrite( "$aRect ERR" & @CRLF )
;~   ConsoleWrite( "$aRect OK" & @CRLF )
;~
;~   ; Activate tab
;~   MouseClick( "primary", $aRect[0]+$aRect[2]/2, $aRect[1]+$aRect[3]/2, 1, 0 )

  ; Set the interface to use the methods under LegacyIAccessible Pattern Methods seen in UIASpy
  Local $pLegacyIAccessiblePattern1, $oLegacyIAccessiblePattern1
  $oTab.GetCurrentPattern($UIA_LegacyIAccessiblePatternId, $pLegacyIAccessiblePattern1)
  $oLegacyIAccessiblePattern1 = ObjCreateInterface($pLegacyIAccessiblePattern1, $sIID_IUIAutomationLegacyIAccessiblePattern, $dtag_IUIAutomationLegacyIAccessiblePattern)
  If Not IsObj( $oLegacyIAccessiblePattern1 ) Then Return ConsoleWrite( "$oLegacyIAccessiblePattern1 ERR" & @CRLF )
  ConsoleWrite( "$oLegacyIAccessiblePattern1 OK" & @CRLF )
  $oLegacyIAccessiblePattern1.DoDefaultAction() ;magic sauce to activate a Chrome tab without MouseClick gleaned from https://stackoverflow.com/a/58472735

EndFunc

example.png

Environment: Windows 10 Home 64-bit 19042.804, Chrome 64-bit Version 88.0.4324.150, AutoIt 3.3.14.5.

Edited by Decibel
Updated environment info.
Link to comment
Share on other sites

  • 11 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...