0

I wrote a macro on VBA using Selenium.ChromeDriver, which opens the site and gives it various actions. But the problem is that if an error occurs in the macro and I click "Stop" in debugging mode, then the browser closes. The problem is that when you close the browser and open it, you have to enter your login and password each time. How can I avoid closing the browser opened via Chrome Driver if the macro ends or when an error occurs? Did so

Public Driver as new ChromeDriver

but that doesn't help, the browser closes.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

0

Need some more code really but some pointers

  1. When error occurs click debug rather than end and the browser should remain open
  2. Don't use .Quit if you want automated browser to remain open at end of sub. However, you will need to grab the session id if you wish to reconnect with that instance, or pass the driver object to any receiving sub. Just remember at some point to Quit.

Here is how you open another tab:

 Option Explicit

'download selenium https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub DownloadFile()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "www.something/login/"

    With d
        .Start "Chrome"
        .get URL
        .FindElementById("name").SendKeys ""
        .FindElementById("email").SendKeys ""
        .FindElementByCss("[type=submit]").Click
        Application.Wait Now + TimeSerial(0, 0, 5) '< better to have a wait condition for something on the post login page
        .ExecuteScript "window.open(" & Chr$(34) & "postloginUrl" & Chr$(34) & ",'_blank');"
        Stop
        .Quit
    End With
End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Thanks for the answer! But can I find out the session id and use it when opening a new browser via Selenium? For example, now, if I open a browser through Selenium Chrome Driver, enter my login and password, and then open a new Chrome instance, then I have to enter my login and password again, although I am authorized in another window on this site. Does this mean that Selenium launches every time a browser with a new session id? – giovanni111 Apr 06 '19 at 18:13
  • Using session id is not opening a new browser it is remote connecting to an existing browser instance using a previously stored _handle_ – QHarr Apr 06 '19 at 18:51
  • Can I do this: open Chrome via Selenium, log in to the site, then open another Chrome window with the help of Selenium and be logged in to the site using the previous browser session? – giovanni111 Apr 06 '19 at 18:59
  • was that what you wanted? – QHarr Apr 06 '19 at 19:47
  • Thanks for the answer! This is not exactly what I'm looking for. I need so that I can open Chrome through Selenium and log in on the site. After that, I need to open another separate Chrome window through Selenium and be already logged in. That is, opening the second Chrome to be already logged in on the site using the session of the first Chrome browser. But I do not know whether it is possible. – giovanni111 Apr 06 '19 at 20:18
  • you may be able to do that with a stored profile and launching that profile. – QHarr Apr 06 '19 at 20:25
  • I have just tested my code and updated for a login based portal I use. Provided I leave some time after login on first tab, the second tab which uses a post login url works just fine i.e. the fact I am logged in is recognised. Can you check you example on this? There may be other factors at play but it certainly can work as I have just tested. – QHarr Apr 06 '19 at 20:39