Hello
I’m going to run a macro 24/7.
As we know, browsers are not designed to run macros 24/7 and there is a problem of memory leakage.
I read in other users’ posts that you have identified the best way to prevent memory leakage, to open and close the browser at a certain time interval. But you did not give any explanation.
Given that I have run several macros via vbs in several different profiles in the Chrome browser.
Several ways to open and close consecutively come to my mind.
1- Using a variable at the end of the macro and checking this variable inside vbs (if this variable is true, close the browser and open it (specified profile))
2- Close the browser at the end of the macro and check the condition (vbs)
If the browser (special profile) was closed, open it again.
3- Using the loop and the sleep command in vbs to open and close (regardless of the macro and its commands) in a specified period of time.
Your opinion is important. What is the best way?
I prefer solution 1 or 2 and have almost found some code to run them but it is not complete.
This piece of code checks every few minutes to open it if the Chrome browser is closed. But the problem is that I want to check a specific profile, not the whole Chrome browser. And I do not know what the code is.
Set objShell = CreateObject("WScript.Shell")
i = 1
strProfile = objShell.ExpandEnvironmentStrings("%LOCALAPPDATA%")
strPath = strProfile & "\Google\Chrome\Application\chrome.exe"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
Do While i = 1
booChrome = False
Set colProcesses = objWMIService.ExecQuery _
("Select * From Win32_Process")
For Each objItem in colProcesses
strProcessName = objItem.Caption
'msgbox strProcessName
If strProcessName = "chrome.exe" Then
booChrome = True
end if
Next
If booChrome = False Then objShell.Run(Chr(34) & strPath & Chr(34))
WScript.Sleep 300000
Loop
The way I do it is to close & restart the browser after e. g. 100 macro runs.
And my code checks the command line for timeout (no response, e. g. because browser is frozen or crashed). If there is a timeout due to browser or UI Vision hanging it kills all Chrome instances. Then automatically (by UI Vision) on the next command line call Chrome is started again.
Basically I use the code from the RPA Github repo , it has all the checks I mentioned:
I suggest you do not use Chrome, but I suggest you to use firefox portable, you copy multiple time the folder with firefox and run it, no need to set profile or other problem.
You can instant run unlimited firefox portable, need only to copy the folder to have multiple firefox (profiles are autoamtically created).
It’s more easy to use firefox portable, you can run it via batch file and there are any settings to do.
With firefox you can run unlimited browser in same time instant (with standard selenium command) and you can have the maximum speed using multiple browser in same time at work.
With chrome this is not possible.
If you have a long csv to use you can split it in more parts and run multiple firefox that use a part of csv in this mode you can complete in fast way every work.
Try to execute a macro with 1000 csv line with chrome require more time (more hours)
Execute this csv splitting it in 5 parts and run 5 firefox portable and you’ll complete the work in 1/5 of time of Chrome
Only firefox can do this and can reduce time of execution using multiple browser in same time.
In the command line you can use -closeBrowser and -closeRPA and this will close the launched profile only. But these commands fails (of course) if the browser or the RPA software itself hangs.
What I do this this:
Launch scripts and browser profiles… …and close them with -closeBrowser and -closeRPA
Every 2 hours I stop (automatically, of course, inside script) every automation. Then I kill all left over “hung” Chrome and Firefox processes. This way I have always a fresh and clean memory, and no orphaned browser instances can pile up.
Your answer is correct, and by “faster” I mean that my site loads faster in the Chrome browser.
And I split the macro into two parts and used the Chrome profile to speed up the macro to finish faster (in general).
This is a good idea (except when the browser is hanged )
Of course, Mr. Plankton’s code snippet does the same. The only problem for me was that it kills the whole browser.
If I use these two commands, there are two way for the vbscript code snippet to notice that the browser and rpa are closed,(one is mr planktons: use log file to determine if a chrome is closed or not) and other is to use a variable at the end of the macro. Vbscript can check the value of this variable every few moments and if there is a specific value (for example, false, which indicates that the browser and rpa is closed), run the browser again with the desired profile (the second code I sent).
Is it possible to call a variable from within vbscript? if yes HOW?
Here’s what I’m doing to close a specific Chrome “profile”:
$scriptName = "myMacro"
$profileArgs = "--flag-switches-begin --user-data-dir=..\profiles\{0} --no-default-browser-check --allow-outdated-plugins --disable-logging --disable-breakpad --flag-switches-end" -f $scriptName
$filterString = "Name='chrome.exe' AND CommandLine LIKE '%{0}%'" -f $scriptName # $profileArgs would be better, but for some reason gives an error
(Get-WmiObject win32_process -filter $filterString).Terminate()
The last two commands will search Chrome’s process with a given “profile” (command line argument) and kill such instance.
You cannot use Chrome’s profile functionality (–profile-directory), you actually have to provide a different –user-data-dir parameter when launching Chrome (see $profileArgs). --profile-directory parameter is lost because Chrome processes merge together (different profiles, but same underlying Chrome instance), --user-data-dir is preserved because processes are not merged (independent Chrome instances)
The following program was used to aid in this analysis:
Someone should just make a CLI app where it reads an INI file and starts Chrome/Firefox instances accordingly:
I think this error is related to the macro name.
Do you mean macro name, macro name in ui.vision or vbscript file name?
If I give this name m, your code will work but it still exit the whole browsers.
Good question: The log file is automatically created after each macro run if you add savelog=Log.txt to the command line.
So this command line log file needs savelog=. It should not be confused with the hard-drive log file, which is created automatically and is simply a copy of whatever you see in the log tab in the GUI.
$scriptName here is used as the “profile name” (–user-data-dir=…\profiles{0}), so in this case you would need to launch Chrome with something like chrome.exe --user-data-dir=master instead of --profile-directory=master
Replace --profile-directory with --user-data-dir. Please note doing so Chrome will start and act as if the profile was empty, because we have actually changed the directory Chrome will be reading (so you may need to install UI.Vision again)
Also, I haven’t tested with spaces in the profile name (“Profile1” vs “Profile 1”)