paolo.lrs
paolo.lrs

Reputation: 41

Selenium VBA - exit sub without close browser window

How can I exit a sub (macro) without close browser window? When code finish Chrome browser automatically close.

For example I have

Sub test()
Dim driver As New ChromeDriver
driver.Get "http://www.google.com"
End Sub

Thanks!

Upvotes: 2

Views: 7033

Answers (1)

Florent B.
Florent B.

Reputation: 42518

The browser is automatically disposed once the variable of the driver is out of scope. For further information, I invite you to have a look at the official documentation: https://support.microsoft.com/en-gb/kb/141693

So, to avoid quitting the browser, you need to set the instance of the driver on a global variable:

Private Assert As New Assert
Private driver As New Selenium.FirefoxDriver

Sub ProcMain()
    driver.Get "http://stackoverflow.com"
    Call ClickLogo
End Sub

Sub QuitDriver()
    driver.Quit
End Sub

Sub ClickLogo()
    driver.FindElementByCss("#hlogo").Click
End Sub

To get the latest version in date working with the above example: https://github.com/florentbr/SeleniumBasic/releases/latest

Upvotes: 5

Related Questions