Jump to content

WinClose Not Closing Chrome


Wannabe
 Share

Recommended Posts

Good morning/afternoon/evening depending on where you are...

I'm writing code that should be shutting down some Chrome windows but not all of them. I've run into an issue where if a Chrome browser window contains more than 2 tabs, it does not obey the WinClose (or WinKill for that matter) commands and I am forced to use SendKeys to accomplish the goal. Is this something I am doing wrong, or something more systemic between Chrome and AutoIT. Also, further note, at this time I'm unable to use the Chrome UDF...

If $isProt = 1 Then
            ConsoleWrite("Chrome Window contains protected tab, ignoring browser..." & @CRLF)
        Else
            ConsoleWrite("Chrome Window contains no protected tabs, closing window...  " & $aOpenChrome[$x][1] & @CRLF)
            Local $closeResult = WinClose($aOpenChrome[$x][1])
            ConsoleWrite("$closeResult = " & $closeResult & @CRLF)
            Sleep(100)
            If WinExists($aOpenChrome[$x][1]) Then
                ConsoleWrite("Uh oh, chrome was supposed to be closed but didn't...." & @CRLF)
                WinActivate($aOpenChrome[$x][1])
                Send("!{F4}")
                ConsoleWrite("Used SendKeys to close chrome window...." & @CRLF)
            EndIf

        EndIf

Here is the console output I am getting:

Quote

 

<A successful protected action:>

Working in HWND: 0x00010C1E
Title is: <System Issue Dashboard>    
address: *************

$nProtected is: 22
@error is: 0
URL of: <http://**************> is protected. Exiting loop...
Chrome Window contains protected tab, ignoring browser...

<A successful close action:>

Working in HWND: 0x00010CF0
Title is: <Learn to code | Codecademy>    
address: https://www.codecademy.com

$closeResult = 1

$nProtected is: -1
@error is: 0
Chrome tab with URL of: https://www.codecademy.com. Not protected...
Chrome Window contains no protected tabs, closing window...  0x00010CF0

<A failed close action:>

Working in HWND: 0x00010D00
Title is: <Intranet>    
address: https://intranet

$nProtected is: -1
@error is: 0
Chrome tab with URL of: https://intranet. Not protected...
Correct Window Active!
Working in HWND: 0x00010D00
Title is: <Intranet>    
address: https://intranet

$nProtected is: -1
@error is: 0
Chrome tab with URL of: https://intranet. Not protected...
Correct Window Active!
Working in HWND: 0x00010D00
Title is: <Intranet>    
address: https://intranet

$nProtected is: -1
@error is: 0
Chrome tab with URL of: https://intranet. Not protected...
Correct Window Active!
Working in HWND: 0x00010D00
Title is: <Intranet>    
address: https://intranet

$nProtected is: -1
@error is: 0
Chrome tab with URL of: https://intranet. Not protected...
Chrome Window contains no protected tabs, closing window...  0x00010D00


$closeResult = 1
Uh oh, chrome was supposed to be closed but didn't....
Used SendKeys to close chrome window....

 

 

Link to comment
Share on other sites

 

13 minutes ago, Earthshine said:

google is your friend

 

Good morning friend,

What you posted is actually a thread I already searched, examined, and attempted. The ProcessClose is shutting down all Chrome browser windows. It could be I'm using it wrong, though. :) 

Link to comment
Share on other sites

I have no idea if this will work: Have you tried getting a process ID with WinGetProcess and the killing the PID? 

If this is a useless suggestion - apologies in advance :-)

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

1 minute ago, SlackerAl said:

I have no idea if this will work: Have you tried getting a process ID with WinGetProcess and the killing the PID? 

If this is a useless suggestion - apologies in advance :-)

I think he does not want to kill Chrome.exe, just some tabs inside it.

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

2 minutes ago, Earthshine said:

so, you want to close only chosen chrome tabs?

Correct. $isProt is determined previously in the Func, if it is determined a tab contains a URL we don't want closed, it ignores it. That seems to be working without issue. Closing tabs is also working, as long as there are only a couple of tabs. For some reason, WinClose and WinKill will not close Chrome if it has more than two.

2 minutes ago, SlackerAl said:

I have no idea if this will work: Have you tried getting a process ID with WinGetProcess and the killing the PID? 

If this is a useless suggestion - apologies in advance :-)

I greatly appreciate any help here, SlackerAI! :) I actually attempted that before posting based on the recommendations of the thread Earthshine posted. That either does not close any Chrome tabs or it closes them all.

Link to comment
Share on other sites

3 minutes ago, Earthshine said:

I think he does not want to kill Chrome.exe, just some tabs inside it.

I'm sorry, that actually isn't entirely correct. I misread. If a URL we want left open is in a browser, on any tab, the whole browser should be left alone. Otherwise, the browser should be closed. My apologies.

Link to comment
Share on other sites

1 minute ago, Earthshine said:

try the other link i referred to above. Junkew's UI Autmation stuff.

Junkew's UIAWrapper.au3 is exactly what I'm using to check all the tabs! :) 

I've been through the thread and I'm not seeing specific to closing, but I think you are correct, killing the window through SendKeys may have to suffice for now. User interference just worries me. :/ I guess it becomes a training issue, not a coding one. :)

Link to comment
Share on other sites

ok, so i did a test with a chrome based browser. same results, nothing closing. i will also test with chrome

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>

; set variables

$browser_exe = "C:\Epic Privacy Browser\Application\epic.exe"
$URL = "http://website.com"
$window_title = "Create Your Website for Free — Website.com - Epic Privacy Browser"
$sleep_time = 500 ; 1.5 hours

; run the browser and pass the URL to it
$iPID = Run($browser_exe & " " & $URL)

; get window handle
$hWnd = WinHandFromPID($iPID, $window_title)
;~ MsgBox("", "", "handle", $hWnd)

WinClose($hWnd)

Func WinHandFromPID($pid, $winTitle = "", $timeout = 8)
    Local $secs = TimerInit()
    Do
        $wins = WinList($winTitle)
;~      _Arrayisplay($wins)
        For $i = UBound($wins) - 1 To 1 Step -1
            If (WinGetProcess($wins[$i][1]) <> $pid) Or (BitAND(WinGetState($wins[$i][1]), 2) = 0) Then _ArrayDelete($wins, $i)
        Next
        $wins[0][0] = UBound($wins) - 1
        If $wins[0][0] Then Return SetError(0, 0, $wins)
        Sleep(1000)
    Until TimerDiff($secs) >= $timeout * 1000
    Return SetError(1, 0, $wins)
EndFunc   ;==>WinHandFromPID

 

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

I have run into the same exact situation. I have several chrome windows/tabs open, I wrote a script that launches a new chrome window and I want that same window to be closed a few moments later. I've tried both WinClose() and Send() to no avail.

#include <AutoItConstants.au3>
#include <Misc.au3>

If _Singleton("frr", 0) = 0 Then
   Exit
EndIf

Opt("MouseCoordMode", 0)
Func MainStuff()
   Run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe /new-window https://somewebsite")
   Sleep(2000)
   ; Do some stuff
   Sleep(5000)
   WinClose("[ACTIVE]", "")
   ; Send("!{F4}")
   Exit
EndFunc

Chrome just refuses to close.

Edited by webjocky

The tiniest of things can ruin your day, or brighten your afternoon. Read it once, read it again, and pay attention. Then search the forums. If all else fails, ask somebody else.

Link to comment
Share on other sites

See how when first pushing a "new tab" It gets a lot more easy to manage
This example not reliable enough to guarantee keeping protected tabs open as it sometimes even closes them
But maybe with a bit more tweaking or sleeps or error checking it can be more accurate,

With what I have tried it mostly worked

#include <WinAPI.au3>

Global $aProtected[5] = [4, "Gmail", "autoit", "Google Search", "YouTube"]

Local $a = WinList("[CLASS:Chrome_WidgetWin_1]")
If Not IsArray($a) Then Exit

For $i = 1 To UBound($a) - 1
    If $a[$i][0] Then _Process($a[$i][1])
Next

Func _Process($hWnd)
    Local $bClose = True, $x = 1, $tmp
    WinActivate($hWnd)
    Local $hC = _WinAPI_GetClassName(_WinAPI_GetWindow($hWnd, $GW_CHILD))
    ControlFocus($hWnd, '', ControlGetHandle($hWnd, "", $hC & 1))
    ControlSend($hWnd, "", $hC & 1, "^t")
    ControlSend($hWnd, "", $hC & 1, "^+{TAB}")
    $oBuffer = ObjCreate('Scripting.Dictionary')

    Do
        $bClose = True
        $tmp = WinGetTitle($hWnd, "")

        If $oBuffer.Exists($tmp) Then
            ControlSend($hWnd, "", $hC & 1, "^w")
        Else
            For $i = 1 To $aProtected[0]
                If StringInStr($tmp, $aProtected[$i]) Then
                    $bClose = False
                    ExitLoop
                EndIf
            Next
            If $bClose Then
                ControlSend($hWnd, "", $hC & 1, "^w")
            Else
                $x += 1
            EndIf
            $oBuffer.Item($tmp) = 1
        EndIf
        Sleep(100)

        ControlSend($hWnd, "", $hC & 1, "^+{TAB}")
    Until Not HWnd(ControlGetHandle($hWnd, "", $hC & $x + 2))

    ControlSend($hWnd, "", $hC & 1, "^+{TAB}")
    If StringInStr(WinGetTitle($hWnd, ""), "New Tab") Then
        ControlSend($hWnd, "", $hC & 1, "^w")
        Sleep(100)
        ControlSend($hWnd, "", $hC & 1, "^{TAB}")
    EndIf
EndFunc   ;==>_Process

 

Link to comment
Share on other sites

This works fine on my system

winclose("[CLASS:Chrome_WidgetWin_1]")

Closing it with iuiautomation stuff you just have to click the closing button right top of window or click 1 by 1 if you want to do it per tab no special things on that observed.

Protected/fixed tabs you can only close with ctrl+w

 

Edited by junkew
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...