To open a webpage and render JavaScript using Excel VBA, you cannot use the standard XMLHTTP request because it only downloads the raw HTML source without executing scripts. Instead, you must use a browser automation tool, most commonly Selenium Basic, which controls a real browser instance (like Chrome or Edge) to fully render the Document Object Model (DOM) after JavaScript has run. This allows you to scrape data from dynamic websites where content is loaded asynchronously via AJAX or React.

What You'll Need

  • Microsoft Excel installed on a Windows PC.
  • The VBA Editor enabled (Developer tab).
  • Selenium Basic wrapper (the open-source library that bridges VBA to browser drivers).
  • A web driver executable (ChromeDriver or EdgeDriver) that matches the exact version of the browser installed on your machine.
  • Administrative privileges to install the Selenium Basic library.

Step-by-Step

  1. Install Selenium Basic: Download and install the Selenium Basic package. This provides the necessary type libraries that allow VBA to communicate with web browsers. During installation, ensure you note the installation directory, as you will need to place the driver files there later.
  2. Match the Browser Driver: Check your browser version (e.g., in Chrome, go to Settings > About Chrome). Download the corresponding driver from the official Chromium or Edge driver repository. For Chrome, download the ChromeDriver.exe; for Edge, download the msedgedriver.exe.
  3. Deploy the Driver: Copy the downloaded .exe driver file and paste it into the Selenium Basic installation folder (typically C:\Users\\YourUser\\AppData\\Roaming\\SeleniumBasic or C:\Program Files\\SeleniumBasic). Rename the file to chromedriver.exe or edgedriver.exe if it isn't already, ensuring it replaces any outdated version.
  4. Enable the Selenium Reference in VBA: Open Excel and press Alt + F11 to open the Visual Basic Editor. Go to the top menu and select Tools > References. Scroll through the list and check the box for Selenium Type Library. If this is not checked, your code will throw a "User-defined type not defined" error.
  5. Initialize the Browser Object: Create a new module and declare a browser variable. Use Dim driver As New Selenium.ChromeDriver (for Chrome) or Dim driver As New Selenium.EdgeDriver (for Edge). This initializes the object that will control the browser window.
  6. Navigate and Render: Use the driver.Get "URL" command to open the webpage. Because Selenium uses a real browser, the page will load exactly as it does for a human user, executing all JavaScript, CSS, and API calls.
  7. Handle Asynchronous Loading: Since JavaScript takes time to render, you must implement a wait mechanism. Do not use Application.Wait, as this freezes Excel. Instead, use driver.Wait 5000 (for a hard wait of 5 seconds) or a loop that checks if a specific element exists on the page before proceeding.
  8. Extract the Rendered Data: Use the FindElementByCss or FindElementById methods to locate the specific HTML element that was generated by the JavaScript. For example, Set element = driver.FindElementById("price-tag"). You can then extract the text using element.Text.
  9. Close the Session: Always end your script with driver.Quit. If you only use driver.Close, the browser window may shut, but the background driver process (chromedriver.exe) will remain running in your system memory, eventually slowing down your computer.

Common Mistakes

The most frequent error is the Version Mismatch. Chrome and Edge update automatically every few weeks. When the browser updates to version 122, but your driver is still version 120, the code will crash immediately upon calling driver.Get. You must manually update the .exe driver file in the Selenium folder whenever the browser updates.

Another common mistake is attempting to use the Internet Explorer (IE) driver. Most modern websites no longer support IE, and Microsoft has retired the browser. Even if you use the Selenium.IEDriver, the page will either fail to render JavaScript correctly or display a "Browser not supported" message.

Many users also forget to set the reference library. If you share your workbook with a colleague, the code will fail on their machine unless they also have Selenium Basic installed and the reference checked in their VBA editor. This makes Selenium-based workbooks less portable than standard VBA scripts.

Finally, failing to account for IFrames is a major hurdle. If the JavaScript renders content inside an iframe, driver.FindElement will return an error because the driver is looking at the main document. You must first use driver.SwitchToFrame "frame_id" before you can interact with elements inside that frame.

Tips

To avoid the visual distraction of a browser window popping up and disappearing, use Headless Mode. You can configure the driver to run in the background by adding driver.SetPreference "headless", "true" (depending on the driver version) or using ChromeOptions. This makes the automation faster and less intrusive.

For better reliability, use Explicit Waits. Instead of guessing how many seconds a page takes to load, create a loop that checks for the existence of a specific element every 500 milliseconds. This ensures the script proceeds the millisecond the JavaScript finishes rendering, rather than waiting for a fixed timer that might be too short or unnecessarily long.

If you need to interact with the page (like clicking a "Load More" button to trigger more JavaScript), use the Click method on the specific element. Combine this with a short wait to allow the new content to populate the DOM before attempting to scrape it.

Use CSS Selectors over IDs whenever possible. IDs can be dynamic (changing every time the page refreshes), whereas CSS classes or hierarchical paths are often more stable for long-term automation scripts.

Troubleshooting

If the browser opens but the script crashes immediately: Check the version of your browser and the version of your driver. This is the cause of 90% of Selenium failures. Go to the browser's "About" page, find the version number, and download the matching driver from the official source.

If the script says "Element Not Found" but you can see it on the screen: The JavaScript is likely still rendering when the VBA code reaches the search command. Increase your wait time or implement a loop that checks for the element's presence. Also, check if the element is inside an iframe; if so, use the SwitchToFrame command.

If the Selenium Type Library is missing from the References list: Reinstall Selenium Basic as an Administrator. If it still doesn't appear, you may need to manually browse for the Selenium.tlb file located in the installation directory.

If Excel freezes or becomes unresponsive: Ensure you are not using Application.Wait in conjunction with a browser loop. Use the Selenium-native driver.Wait or a DoEvents call within your loop to keep the Excel UI responsive while the browser works in the background.

Frequently Asked Questions

Can I do this without installing third-party software?

No. Standard VBA (using XMLHTTP or WinHTTP) cannot execute JavaScript. To render JS, you need a browser engine, which requires a driver like Selenium or a commercial API service.

Does this work on Mac?

No. Selenium Basic is designed for Windows. Mac users would need to use Python with Selenium or a different automation tool, as the VBA library for Selenium does not support macOS.

Will this work for websites that require a login?

Yes. Because Selenium controls a real browser, you can use driver.FindElementById("username").SendKeys "myuser" to log in, or simply manually log in during a long pause in the script, and the session cookies will be maintained.

How do I handle pop-ups or alerts?

You can use driver.SwitchToAlert().Accept to click "OK" on a JavaScript alert box, or find the "X" button on a HTML modal pop-up and use the Click method.

Is this the same as web scraping with Power Query?

No. Power Query is excellent for static tables, but it often struggles with heavily JavaScript-rendered pages. Selenium is more powerful for dynamic content but requires more setup and maintenance.