Jump to content

Closing GUI when browser session is closed


 Share

Recommended Posts

Something I thought would be so simple is really taking up so much time figuring out.

Does anyone have a trick (or I missed some documentation) on how to immediately close my GUI after I closed the browser I opened in Webdriver?

This is where I'm at now:

#include <WindowsConstants.au3> ;For $WS_EX_ACCEPTFILES
#include <GUIConstantsEx.au3>   ;For $GUI_EVENT_CLOSE

$RequiredFolder = @ScriptDir & '\WebDriver\' ;FIX THIS IF YOU CHANGE REQUIRED FOLDER as well as includes below
#include ".\WebDriver\wd_core.au3"
#include ".\WebDriver\wd_helper.au3"
Global $hGUI, $hIcon, $hStatus, $nowURL, $preURL
Global $sDesiredCapabilities,$_WD_DEBUG
HotKeySet("{ESC}","_Exit") ;Press ESC key to quit


CreateGUI()
SetupChrome() 
_WD_Startup() ;See Func SetupChrome for headless and other $sDesiredCapabilities
$sSession = _WD_CreateSession($sDesiredCapabilities)
;----Navigating to Page
_WD_Navigate($sSession, 'https://www.google.com') 
_WD_LoadWait($sSession, 200,5000)

$nowURL=_WD_Action($sSession, 'url')
While StringLen($nowURL)>0
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
         ExitLoop
  EndSwitch
  $nowURL=_WD_Action($sSession, 'url')
WEnd
_Exit()

Func CreateGUI()
    $hGUI=GUICreate('I will shutdown when Chrome shuts down', 600, 500, -1, -1, -1)
    $hStatus = GUICtrlCreateEdit("",5,60,590,435)
    GUISetState(@SW_SHOW, $hGUI)
EndFunc ;==>CreateGUI

Func _Exit()

    _WD_DeleteSession($sSession)
    _WD_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc   ;==>Exit

Func SetupChrome()
   If Not FileExists($RequiredFolder & "chromedriver.exe") Then
      SplashTextOn("Downloading Webdriver","Webdriver was not found. Downloading chrome webdriver.")
      Sleep(3000)
      SplashOff()
      $wasWDupdated=_WD_UpdateDriver('Chrome', $RequiredFolder, False, True);Auto-update webdriver
   Else
      $wasWDupdated=_WD_UpdateDriver('Chrome', $RequiredFolder);Auto-update webdriver
      If $wasWDupdated Then
         SplashTextOn("Webdriver Updated","Your webdriver was old. It was automatically updated to match the version of Chrome you're using")
         Sleep(3000)
         SplashOff()
      EndIf
   EndIf

   _WD_Option('Driver', $RequiredFolder & 'chromedriver.exe')
   _WD_Option('Port', 9515)
   _WD_Option('DriverParams', '--verbose --log-path="' & $RequiredFolder & 'chrome.log"')
   _WD_Option('DriverDetect', False) ; To ignore another instance of Chrome
   $_WD_DEBUG = $_WD_DEBUG_None ;Must be called before startup or-> $_WD_DEBUG = $_WD_DEBUG_INFO"
    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"unhandledPromptBehavior": "ignore", "goog:chromeOptions": {"w3c": true, "excludeSwitches": ["enable-automation"], "useAutomationExtension": false, "prefs": {"credentials_enable_service": false}, "args": ["window-size=1060,' & @DesktopHeight-34 & '","window-position=0,0"] }}}}' ;300,85
EndFunc

 

Thanks for any tips or help!

Edited by NassauSky
Link to comment
Share on other sites

  • NassauSky changed the title to Closing GUI when browser session is closed
  • $_WD_DEBUG is declared as a global variable in wd_core, so no point in declaring it again
  • If you set $_WD_DEBUG = $_WD_DEBUG_Error, then this may help you understand the delay in closing
  • You should be able to reduce the delay by changing the contents of the global array $_WD_HTTPTimeOuts, ie:
    $_WD_HTTPTimeOuts[1] = 1000 ; Connect timeout is now one second

     

Link to comment
Share on other sites

Nice to know @Danp2

 

OK before setting the timeout I was curious what was slowing down the shutting down of the Gui maybe 15 to 30 seconds after closing Chrome. 

Using $_WD_DEBUG = $_WD_DEBUG_Error but the console isn't displaying something that I can make heads or tails of:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "E:\Cool\Autoit\Scripts\WebdriverSample\Test.au3"    
_WDStartup: "E:\Cool\Autoit\Scripts\WebdriverSample\WebDriver\chromedriver.exe" --verbose --log-path="E:\Cool\Autoit\Scripts\WebdriverSample\WebDriver\chrome.log" 
__WD_Get ==> Webdriver Exception: {"value":{"error":"chrome not reachable","message":"chrome not reachable\n  (Session info: chrome=98.0.4758.102)","stacktrace":"Backtrace:\n\tOrdinal0 [0x00C869A3+2582947]\n\tOrdinal0 [0x00C1A6D1+2139857]\n\tOrdinal0 [0x00B13960+1063264]\n\tOrdinal0 [0x00B08092+1015954]\n\tOrdinal0 [0x00B088C8+1018056]\n\tOrdinal0 [0x00B0A102+1024258]\n\tOrdinal0 [0x00B03DE9+998889]\n\tOrdinal0 [0x00B14DD0+1068496]\n\tOrdinal0 [0x00B67B12+1407762]\n\tOrdinal0 [0x00B58366+1344358]\n\tOrdinal0 [0x00B35176+1200502]\n\tOrdinal0 [0x00B36066+1204326]\n\tGetHandleVerifier [0x00E2BE02+1675858]\n\tGetHandleVerifier [0x00EE036C+2414524]\n\tGetHandleVerifier [0x00D1BB01+560977]\n\tGetHandleVerifier [0x00D1A8D3+556323]\n\tOrdinal0 [0x00C2020E+2163214]\n\tOrdinal0 [0x00C25078+2183288]\n\tOrdinal0 [0x00C251C0+2183616]\n\tOrdinal0 [0x00C2EE1C+2223644]\n\tBaseThreadInitThunk [0x75F8FA29+25]\n\tRtlGetAppContainerNamedObjectPath [0x77727A7E+286]\n\tRtlGetAppContainerNamedObjectPath [0x77727A4E+238]\n"}}
_WD_Action ==> Webdriver Exception: HTTP status = 500
>Exit code: 0    Time: 9.429

 

 

Using $_WD_DEBUG = $_WD_DEBUG_INFO instead gives more info which might not be enough information since oddly the console freezes and doesn't show a debug result until a moment before the GUI closes:

 

178: __WD_Get: URL=HTTP://127.0.0.1:9515/session/64fb36580bb73154a2c4c51aa7c39812/url
__WD_Get: StatusCode=200; $iResult = 0; $sResponseText={"value":"https://www.google.com/"}...
_WD_Action: {"value":"https://www.google.com/"}
179: __WD_Get: URL=HTTP://127.0.0.1:9515/session/64fb36580bb73154a2c4c51aa7c39812/url
__WD_Get: StatusCode=200; $iResult = 0; $sResponseText={"value":"https://www.google.com/"}...
_WD_Action: {"value":"https://www.google.com/"}
180: __WD_Get: URL=HTTP://127.0.0.1:9515/session/64fb36580bb73154a2c4c51aa7c39812/url
__WD_Get: StatusCode=200; $iResult = 0; $sResponseText={"value":"https://www.google.com/"}...
_WD_Action: {"value":"https://www.google.com/"}
181: __WD_Get: URL=HTTP://127.0.0.1:9515/session/64fb36580bb73154a2c4c51aa7c39812/url


*Here is where it sometimes pauses from displaying to the console for maybe a second to 20 seconds after which it then continues displaying the Debug information below and shuts down the GUI. 



__WD_Get: StatusCode=500; $iResult = 10; $sResponseText={"value":{"error":"chrome not reachable","message":"chrome not reachable\n  (Session info: chrome=98...
__WD_Get ==> Webdriver Exception: {"value":{"error":"chrome not reachable","message":"chrome not reachable\n  (Session info: chrome=98.0.4758.102)","stacktrace":"Backtrace:\n\tOrdinal0 [0x00C869A3+2582947]\n\tOrdinal0 [0x00C1A6D1+2139857]\n\tOrdinal0 [0x00B13960+1063264]\n\tOrdinal0 [0x00B08092+1015954]\n\tOrdinal0 [0x00B088C8+1018056]\n\tOrdinal0 [0x00B0A102+1024258]\n\tOrdinal0 [0x00B03DE9+998889]\n\tOrdinal0 [0x00B14DD0+1068496]\n\tOrdinal0 [0x00B67B12+1407762]\n\tOrdinal0 [0x00B58366+1344358]\n\tOrdinal0 [0x00B35176+1200502]\n\tOrdinal0 [0x00B36066+1204326]\n\tGetHandleVerifier [0x00E2BE02+1675858]\n\tGetHandleVerifier [0x00EE036C+2414524]\n\tGetHandleVerifier [0x00D1BB01+560977]\n\tGetHandleVerifier [0x00D1A8D3+556323]\n\tOrdinal0 [0x00C2020E+2163214]\n\tOrdinal0 [0x00C25078+2183288]\n\tOrdinal0 [0x00C251C0+2183616]\n\tOrdinal0 [0x00C2EE1C+2223644]\n\tBaseThreadInitThunk [0x75F8FA29+25]\n\tRtlGetAppContainerNamedObjectPath [0x77727A7E+286]\n\tRtlGetAppContainerNamedObjectPath [0x77727A4E+238]\n"}}
_WD_Action: {"value":{"error":"chrome not reachable","message":"chrome not reachable\n  (Session info: chrome=98.0.4758.102)","stacktrace":"Backtrace:\n\tOrdinal0 [0x00C869A3+2582947]\n\tOrdinal0 [0x00C1A6D1+2139857]\n\tOrdinal0 [0x00B13960+1063264]\n\tOrdinal0 [0x00B08092+1015954]\n\tOrdinal0 [0x00B088C8+1018056]\n\tOrdinal0 [0x00B0A102+1024258]\n\tOrdinal0 [0x00B03DE9+998889]\n\tOrdinal0 [0x00B14DD0+1068496]\n\tOrdinal0 [0x00B67B12+1407762]\n\tOrdinal0 [0x00B58366+1344358]\n\tOrdinal0 [0x00B35176+1200502]\n\tOrdinal0 [0x00B36066+1204326]\n\tGetHandleVerifier [0x00E2BE02+1675858]\n\tGetHandleVerifier [0x00EE036C+2414524]\n\tGetHandleVerifier [0x00D1BB01+560977]\n\tGetHandleVerifier [0x00D1A8D3+556323]\n\tOrdinal0 [0x00C2020E+2163214]\n\tOrdinal0 [0x00C25078+2183288]\n\tOrdinal0 [0x00C251C0+2183616]\n\tOrdinal0 [0x00C2EE1C+2223644]\n\tBaseThreadInitThunk [0x75F8FA29+25]\n\tRtlGetAppContainerNamedObjectPath [0x77727A7E+286]\n\tRtlGetAppContainerNamedObjectPath [0x77727A4E+238]\n"}}
_WD_Action ==> Webdriver Exception: HTTP status = 500
__WD_Delete: URL=HTTP://127.0.0.1:9515/session/64fb36580bb73154a2c4c51aa7c39812
__WD_Delete: StatusCode=200; ResponseText={"value":null}...
_WD_DeleteSession: {"value":null}
>Exit code: 0    Time: 14.14

 

Does it make sense to you? 

Thanks.

Edited by NassauSky
Link to comment
Share on other sites

Thanks @Danp2. I have a higher priority right now but for the most part the shutting down of the GUI isn't terrible for now.

Another tougher problem.   I wanted to upload files from selected staff files to a website which has a full page drag and drop feature. For example if I want to use an Autoit GUI for staff selection and drag in the file selected to for example Office365 web portal, how may I proceed?  Office365 allows me to drag from file explorer and it will open the file but it doesn't function if I try to paste it into the browser window.

Don't know if I'm on the right track but looking at this web developer's blog: https://web.dev/read-files/#select-dnd , it seems that the website might be using javascript to handle the drag and dropping events which then makes me ask what the trick (or tips) for the flow of data between explorer and websites like this.

Make sense?

Thanks again

Edited by NassauSky
Link to comment
Share on other sites

Come to think of it.  What verbiage am I looking for while I search for help on Webdriver that will guide me to have Webdriver open Chrome using it's existing authentication  credentials saved. Each time I connect to chrome it opens in a clean environment and I have to add code in autoit with my personal log-in credentials which only allows them access to my web account. I'd like each user to be able to access their own web account when Webdriver hooks up to their web documents.

Link to comment
Share on other sites

Plugging away I am thinking it's related to Chrome profiles. So far tried changing my desired capabilities:

 

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"unhandledPromptBehavior": "ignore", "goog:chromeOptions": {"w3c": true, "excludeSwitches": ["enable-automation"], "useAutomationExtension": false, "prefs": {"credentials_enable_service": false}, "args": ["user-data-dir=userData"] }}}}' ;300,85

 

Result:

Failed to create data directory:
Google Chrome cannot read and write to its data directory:
userData

 

 

Link to comment
Share on other sites

Got it.  When I first tried what you mentioned @Danp2 

 

in an older post, it didn't work and showed a similar error as before:

Turns out @UserName does not match the directory username that was on my Win10 PC.  Not sure why but I manually added it in as shown here:

;$UserDataDir = 'C:/Users/' & @UserName & '/AppData/Local/Google/Chrome/User Data/'
$UserDataDir = 'C:/Users/' & 'mickey' & '/AppData/Local/Google/Chrome/User Data/'
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"unhandledPromptBehavior": "ignore", "goog:chromeOptions": {"w3c": true, "excludeSwitches": ["enable-automation"], "useAutomationExtension": false, "prefs": {"credentials_enable_service": true}, "args": ["user-data-dir=' & $UserDataDir & ', profile-directory=Default"] }}}}' ;300,85

 

Edited by NassauSky
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...