GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Browser Detach Bug

GCuser99 opened this issue · comments

commented

A very strange bug occurs when combining the browser detach option of WebCapabilities class with a command window style that is NOT vbHide or vbMinimizedNoFocus. If any one of the other command window styles is used, then the browser closes on shutdown. Happens with both Chrome and Edge...

Sub test_detach_browser()
    'use this if you want browser to remain open after shutdown clean-up - only for Chrome/Edge
    Dim driver As SeleniumVBA.WebDriver
    Dim caps As SeleniumVBA.WebCapabilities
    
    Set driver = SeleniumVBA.New_WebDriver
    
    'for detach to function properly, must use either vbHide (Default) or vbMinimizedNoFocus
    'driver.CommandWindowStyle = vbHide '<-- this works fine
    driver.CommandWindowStyle = vbNormalFocus '<-- using this will close the browser on shutdown
    
    driver.StartChrome
    
    Set caps = driver.CreateCapabilities(initializeFromSettingsFile:=False)
    
    'this sets whether browser is closed (false) or left open (true)
    'when the driver is sent the shutdown command before browser is closed
    'defaults to false
    'only applicable to edge/chrome browsers
    caps.SetDetachBrowser True
    
    driver.OpenBrowser caps
    
    driver.NavigateTo "https://www.wikipedia.org/"
    
    driver.Wait 1000
    
    'driver.CloseBrowser 'detach does nothing if browser is closed properly by user
    driver.Shutdown
End Sub