Jump to content
Sign in to follow this  
xuzo

How to activate specific tabs in Google Chrome?

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)

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Luigi
      Greetings,
      Is possible build a script to send/receive commands in "Native Messagin" ?

      If you install this extension:
      https://chrome.google.com/webstore/detail/native-messaging-example/mefhabneoickhhbiglkmehhmemefkopf/related
      And this complement:
      https://github.com/Mandar-Shinde/Google-Chrome-Native-Messaging/releases/download/V1.0/GoogleNativeClient.exe

      You have a work example to send / receive messages from Google Chrome's plugin and NativeMessagingApp.exe.

      I found the source NativeMessagingApp.exe's here:
      https://github.com/Mandar-Shinde/Google-Chrome-Native-Messaging
      I try build this script... But not work...
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_Outfile=C:\Users\Public\GoogleNativeClient\NativeMessagingApp2.exe #AutoIt3Wrapper_UseUpx=y #AutoIt3Wrapper_Change2CUI=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #NoTrayIcon #include-once #include <Array.au3> #include <JSMN.au3> ; https://github.com/chechelaky/AutoIt/blob/master/JSMN.au3 #include <Debug.au3> Global $msg = "@empty" Global Const $CLIENT_START = '{"text":"Client Started"}' Global Const $CLIENT_STOP = '{"text":"Client Stopped"}' Global Const $CLIENT_CLICK = '{"text":"Client Click"}' _DebugSetup("NativeMessagingApp2", True, 4, @ScriptDir & "\NativeMessagingApp2.log", True) Global $read, $json, $out = "" If IsArray($CmdLine) Then ;~ $msg = @LF & "Google Chrome Native Messaging App V1" & @LF & @LF & _ ;~ "GPLV3 software distributed under the License is distributed on an ""AS IS"" BASIS" & @LF & _ ;~ "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." & @LF & _ ;~ "See the License for the specific language governing permissions and" & @LF & _ ;~ "limitations under the License." & @LF & @LF ;~ Local $size = UBound($CmdLine, 1) - 1 ;~ $msg &= "App running with args=" & $size & @LF ;~ For $ii = 0 To $size ;~ $msg &= $ii & " : " & $CmdLine[$ii] & @LF ;~ Next ;~ _DebugOut($msg) EndIf sendMessage($CLIENT_START) While Sleep(10) $read = ConsoleRead() If @error And $out Then _DebugOut("$read...............: " & $out) $json = Jsmn_decode($out) If @error Then _DebugOut("$json...............: @Error") Else _DebugOut("$json...............: @Ok") EndIf If IsObj($json) And $json.Exists("text") Then sendMessage('{"text":"' & $json.Item("text") & '"}') Else sendMessage('{"text":"@Error"}') EndIf $out = "" Else $out &= $read EndIf WEnd Func sendMessage($var) ConsoleWrite($var & @CRLF) _DebugOut("sendMessage===>" & $var) EndFunc ;==>Say  
      Edit the file:
      C:\Users\Public\GoogleNativeClient\com.google.chrome.example.echo-win.json

      And replace:
      "path": "NativeMessagingApp.exe",
      By:
      "path": "NativeMessagingApp2.exe",
      Then plugin call the AutoIt script...

      But is not functional...

       
      About the file mainwindow.cpp, have this function:
      inline QString sendMessage(QString data){ unsigned int len = data.length(); cout.write(reinterpret_cast<const char *>(&len), 4); cout << data.toUtf8().data() << flush; return ""; } How re-write this to AutoIt?
      How execute a flush?



      Best regards.
    • By gahhon
      I am not very sure if I am all right to post here tho. Due to other forums have no respond at all.
      So I have my custom make google chrome extension, and it will load unpack into Portable Google Chrome, but it keep pop up the alert box

      Is there any way to disable it? Because my Portable Google Chrome will load as Kiosk mode, so that user can't be uninstall/disable the extension.
      Many thanks in advance.
    • By Seminko
      I use Chrome's 'Translate this page' functionality quite a lot at work. My workflow is usually translating the page to english so I know what it is about, finding correct lines, translating it back to the original language so I can grab the correct XPath (since translating it, updates the structure and most importantly the text displayed, obviously), and rinse and repeat.
      Since I do it all the time, each unnecessary click in the end amounts to quite some time I would rather be spending actually working. You have to click the translate button, it asks whether you want to translate the page, you have to click yes. Then sometimes the little window is in the way so you click it off but when you need to translate back to the original language you have to click the little icon again.
      How would one go about automating it?
      Checked the forum and it seems that the old Chrome UDF doesn't work and the best way for interacting with Chrome is the WebDriver UDF which, from what I've read and tested a bit, creates a new, different instance of Chrome.
      I tried using the good old MouseClick but I don't find it reliable. Also ControlClick seems to be out of the picture since the whole Chrome window is "one single control".
      Any ideas are appreciated.
    • By flashcoder
      Hello,
      I'm wanting catch all stored urls in Google Chrome and as example I had found a code that promisse return all logins (with passwords) stored on Chrome database, but this example don't have worked for me. 
      PS: I have several logins stored in Chrome database
      Some have a idea about this, because this don't work?
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Version=beta #AutoIt3Wrapper_UseUpx=n #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Res_Language=1080 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <MsgBoxConstants.au3> #include <CryptProtect.au3> #include <sqlite.au3> #include <sqlite.dll.au3> Opt("TrayIconHide",1) $file=@scriptdir &"\passwords.txt" if fileexists($file) then filedelete($file) $file_handle= fileopen($file ,1) if $file_handle<>-1 then filewrite($file_handle, chrome() ) fileflush($file_handle) fileclose($file_handle) endif func chrome() local $q, $r, $pwds,$fn=envget("localappdata") & "\google\chrome\user data\default\login data" if fileexists($fn)=false then return "" _sqlite_startup() _sqlite_open($fn) _sqlite_query(-1, "select * from logins;", $q) while _sqlite_fetchdata($q, $r) = 0 $pwds = $pwds & "url: "& $r[0] & @crlf &"usr: "& $r[3] & @crlf &"pwd: "& _CryptUnprotectData( $r[5],"") & @crlf & @crlf wend _sqlite_close() _sqlite_shutdown() return $pwds endfunc  
    • By J2TeaM
      When I was trying to download SciTE, Google Chrome show an error (warning) page. I know it is false positive, but I think we need to report to Google to remove this warning page.

      Download link.
×
×
  • Create New...