2

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!

Community
  • 1
  • 1
paolo.lrs
  • 41
  • 1
  • 2
  • 5

1 Answers1

5

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

Florent B.
  • 41,537
  • 7
  • 86
  • 101