How to check "no such window: target window already closed" in VBA code? #96
-
In case I manually close the browser that is started by SeleniumVBA (by clicking the right upper corner "X"). And then I continue to use the "driver" to navigate to some URL (i.e. driver.NavigateTo XXX ) it will fail and i got the message in immediate window. So, is there a way to check if the browser is closed? or more specifically to check if "no such window: target window already closed" programmatically in VBA? By the way, for now, this is how i handle this situation. Wonder if there is a more elegant way to do this.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @GHRyunosuke, I would suggest you create a specific procedure like NavigateToFailsafe containing the error management logic, checking for the specific Err.Number and/or Err.Description. |
Beta Was this translation helpful? Give feedback.
-
@GHRyunosuke, in addition to @6DiegoDiego9's great suggestion, in your error block I don't think you need to shutdown and then start back up the driver object - try this instead: On Error Resume Next
driver.NavigateTo strURL
If Err.Number <> 0 Then
driver.CloseBrowser
driver.OpenBrowser
driver.NavigateTo strURL
End If
On Error GoTo 0 And I don't know if it is any better than the approach that you are currently taking, but here is an alternative way using two windows (see Managing Windows in the Wiki for more info): driver.StartChrome
driver.OpenBrowser
'create and activate another window - call it the "navigation" window
driver.Windows.SwitchToNew svbaWindow
driver.NavigateTo "https://www.google.com/"
If driver.Windows.Count = 1 Then
'the navigation window was closed without using window.CloseIt method
'Selenium WebDriver thinks the closed window is active so we need
'to activate the open spare window
driver.Windows.SwitchToNext
'then (optionally) open and activate a new "navigation" window
'(if needed - could just use the existing window too!)
driver.Windows.SwitchToNew svbaWindow
End If
driver.NavigateTo "https://www.google.com/"
driver.CloseBrowser
driver.Shutdown PS: I do not know of a way to detect the situation where a window gets closed non-programmatically like in your example, other than trapping the resulting error. For the browser session to remain valid, there must be at least one open window, as far as I know. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot, @6DiegoDiego9 and @GCuser99. I think I will take the easier approach combining both your advice as follows to handle this situation.
|
Beta Was this translation helpful? Give feedback.
Thanks a lot, @6DiegoDiego9 and @GCuser99. I think I will take the easier approach combining both your advice as follows to handle this situation.