person taking white paper from folder

How to use different Chrome profiles in Selenium VBA?

Hey! How are you doing? All good? Hope so…

Anyways, I’m not sure I’m the right guy to help you with all the problems you face in your life, but I am the perfect guy that helps you with your tech problems.

In this post I’m going to teach you how to use the Chrome profiles properly so you can automate the s**t out of everything.

In VBA Tutorial P3 you’ve learned about how you can mage a basic Google scraper. If you recall, I specified nothing about any Google profile because that’s something particular that requires a post on its own.

There are 3 states of Chrome profiles available with Selenium:

  • New empty profile (default);
  • Your personal profile;
  • Custom profile.

New empty profile (default)

By default, just as it is in my VBA Tutorial for Beginners series, launching a Chrome browser without specifying the profile will launch a browser with an empty profile!

Pros of using the default profile:
  • easy to launch;
  • you don’t have to worry about historical data (e.g. visited websites, cookies, passwords) as everything gets deleted once the created instance is closed (which happens when the macro ends);
  • it doesn’t interrupt you working with Chrome in another instance (your personal profile).
Cons of using the default profile:
  • it doesn’t keep track of any data, therefore your passwords are not saved, nor your active logins;
  • cookie pop-ups can be a pain in the a**;
  • installed extensions are not available;
  • you need to handle website logins every time you launch the macro (if logins are required), which can bring delays in testing or delivering results.

Your personal profile

If your code needs access to extensions or if you don’t want to add your login details every single time (of course, you can add a function that does that for you, but it will increase the run time), you might consider using your personal chrome profile. This means that the macro will use the exact profile you’re using when you use Chrome.

To access your personal account through coding you just need to add the following first 2 lines right before the start line:

driver.AddArgument ("--user-data-dir=C:\Users\" & LCase(Environ("username")) & "\AppData\Local\Google\Chrome\User Data")
driver.AddArgument ("profile-directory=Default")
driver.Start "Chrome"
Pros of using your personal profile:
  • your logins are saved, therefore your macro can access any website you are currently logged in;
  • your extensions are enabled, so if you need to access an internal work tool via browser that requires having an extension enabled, you can do it;
  • any changes you make to your profile (e.g. staying logged in on a new website) are available for the macro as well.
Cons of using your personal profile:
  • it can only run one instance at a time, therefore you cannot use your profile while the macro is running;
  • it requires some extra coding (not too much, though);
  • any browsing your macro does is saved, therefore you can get banned if you visit a website too many times as that website might detect your traffic as being unusual.

Custom profile

There are advantages and disadvantages for both sides so far, don’t you think? That’s why the third option exists.

Basically, you just need to transform your personal profile into a custom profile. To do this, you need to actual copy your personal profile in a different location and then point VBA towards that.

So, you just need to go to the location of your current chrome profile (whish should be located in C:\Users\<yourusername> \AppData\Local\Google\Chrome), copy the folder User Data and paste it wherever you want. I used Documents profile for this.

From a coding point of view, the procedure of starting Chrome in a custom profile is exactly the same. You just need to change the location.

driver.AddArgument ("--user-data-dir=C:\Users\" & LCase(Environ("username")) & "\Documents\User Data")
driver.AddArgument ("profile-directory=Default")
driver.Start "Chrome"
Pros of using a custom profile:
  • most of your logins are saved, but some might require a re-login. However, once you do it, it will stay saved;
  • all your extensions are in place;
  • you can use your personal profile as well.
Cons of using a custom profile:
  • if you make any changes in your personal profile (e.g. you change your password on a website and you log in again to it) are not saved. You need to repeat the copy-paste to update the custom profile as well (as a workaround, you can put a Stop right after driver start command, manually do the changes and the re-run the macro);
  • is starts with your personal profile, so you can still get banned for that unusual traffic part, but the good thing is that you can log in to chrome using a different account so your personal account is not affected.
Notes:

1. You need to know that websites not only use profiles for detecting robots, they also use the IPs, locations and so on, therefore you still might get banned for unusual traffic regardless which profile you use. As a workaround, you could add some delays from time to time so the website won’t detect your robot.

Application.Wait (Now + TimeValue("00:00:01")) 'This is for adding one second delay

2. If you are using a shared computer, you might have multiple Google Chrome profiles and every time you manually open Chrome, you need to choose your profile. To solve this, change the Default part from <profile-directory=”Default”> with the name of your profile.

I hope you found some useful info here! See ya next time!


PS: If you want to support this website, you can do it through the Show Your Support page!


Leave a Reply