1. Install Selenium Basic
Selenium Basic is a wrapper that allows you to use Selenium with VBA.
-
Download Selenium Basic:
- Go to the Selenium Basic GitHub page: https://github.com/florentbr/SeleniumBasic/releases.
- Download the latest release (SeleniumBasic-x.x.x.x.exe).
-
Install Selenium Basic:
- Run the downloaded .exe file.
- Follow the installation instructions.
- By default, Selenium Basic will be installed in:
C:\Users\<YourUsername>\AppData\Local\SeleniumBasic.
2. Download ChromeDriver
You need a driver compatible with your Chrome browser version.
-
Determine Your Chrome Version:
- Open Chrome and go to chrome://settings/help.
- Note the version number (e.g., 116.0.5845.96).
-
Download the Compatible ChromeDriver:
- Go to the official ChromeDriver download page: https://googlechromelabs.github.io/chrome-for-testing/#stable.
- Navigate to the "Stable Channel" section.
- Select the appropriate driver for your operating system (e.g., Windows 64-bit).
-
Place the ChromeDriver:
- Extract the downloaded ZIP file.
- Copy the chromedriver.exe file.
- Place it in the Selenium Basic installation folder:
C:\Users\<YourUsername>\AppData\Local\SeleniumBasic.
3. Add Selenium Basic to VBA References
- Open Excel or any Office application that supports VBA (e.g., Word or Access).
- Open the VBA editor (Alt + F11).
- Go to Tools > References.
- Look for Selenium Type Library in the list.
- Check the box and click OK.
4. Verify the Installation
To confirm everything is set up correctly:
- Open the VBA editor and create a new module.
-
Paste the following test code:
Sub TestSelenium() Dim driver As New WebDriver driver.Start "chrome" driver.Get "https://www.google.com" MsgBox "Google has been opened!" driver.Quit End Sub
- Run the macro (F5).
- If Chrome opens Google, the setup is successful.
Code Breakdown:
Sub TestSelenium()
- Purpose: This line starts the subroutine named TestSelenium.
- A subroutine in VBA is a block of code that performs a specific task.
Dim driver As New WebDriver
- Purpose: Declares a variable driver as a new instance of the Selenium WebDriver class.
- WebDriver is the main interface in Selenium Basic used to control and interact with web browsers.
driver.Start "chrome"
- Purpose: Starts a new browser session in Google Chrome.
- "chrome" specifies the browser to use. Selenium Basic supports multiple browsers like Chrome, Firefox, and Edge.
- Behind the Scenes:
- Selenium Basic interacts with the ChromeDriver.exe file installed earlier.
- It launches Chrome using the chromedriver.exe.
driver.Get "https://www.google.com"
- Purpose: Navigates the browser to the specified URL, in this case, Google's homepage (https://www.google.com).
- The Get method loads the webpage by sending an HTTP GET request to the URL.
MsgBox "Google has been opened!"
- Purpose: Displays a message box to the user with the text "Google has been opened!".
- This is used to confirm that the script has successfully opened the Google website.
driver.Quit
- Purpose: Closes the browser and terminates the WebDriver session.
- Why Important?: Ensures that the browser process does not remain running in the background, which can consume system resources.
End Sub
- Purpose: Marks the end of the TestSelenium subroutine.
Execution Flow:
- Initialize: Create a new instance of the WebDriver.
- Launch Chrome: Open a new Chrome browser session.
- Navigate: Load the Google homepage.
- Notify User: Display a confirmation message to the user.
- Cleanup: Close the browser and end the WebDriver session.
Expected Output:
- Chrome browser opens automatically.
- It navigates to Google’s homepage.
- A message box appears with the text: "Google has been opened!".
- When you close the message box, the Chrome browser session also closes.