Manipulating Webpage Actions With VBA (via Selenium & Firefox Geckodriver)

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,604
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Does anyone here have experience with manipulating browser (Firefox) input via Excel VBA. Is it even possible?
I have been working on some test code that uses Selenium and Geckodriver to try and accomplish this. Looking to access a webpage, populate it's search field, and execuate the search.
I've accessed the Firefox prerequisistes. I have a valid version (v133) of Firefox, v0.35 of geckodriver, and I have installed Selenium 4.27.1 via Python. I originally installed the Selenium Basic, but realized it wasn't supported by geckodriver. I installed Python hoping that the Python based Selenium (?) would be what I needed. Not sure though now. The only difference I found in configuring everything, was there are no libraries to add to Excel references unlike the Basic vesion.

here is my code I'm experimenting with:
Rich (BB code):
Sub OpenWebsiteInFirefox()
    ' Declare Selenium variables
   Dim driver As New Selenium.WebDriver
    Dim searchBox As Object
    Dim geckopath As String
    Dim searchTerm As String
 
    geckopath = "C:\Operations\geckodriver.exe"
    searchTerm = "Historic Chess Masters"
  
    ' Start Firefox browser
    driver.Start "firefox", geckopath
   
    ' Navigate to the website (for example, Google)
    driver.Get "https://www.drinkwillibald.com/"
 
    ' Wait for the page to load (adjust the sleep time as needed)
    driver.Wait 2000
 
    ' Find the search box using its name or ID
    Set searchBox = driver.FindElementByName("q")
 
    ' Type in the search box
    searchBox.SendKeys "Excel VBA automation"
 
    ' Execute the search by pressing Enter
    searchBox.SendKeys Keys.Enter
 
    ' Wait for results to load (optional)
    driver.Wait 2000
End Sub

The line in red stops the code with an error: "User-defined type not defined

I had gotten closer with this code when I was using Selenium basic. It would open up a Firefox window, but would timeout on the line in purple. Error: "TimeoutErrorFirefox failed to open the listening port 127.0.0.1:12825 with 15s" (I didn't get the error associated with the red line).

Grateful to hear from others with experience with this concept of VBA based webpage manipulation in whatever method (if any) that works. I am prepared for this method not to work, but I hope there are other options if not.

I did cross post (more detailed) at the Selenium user support page, but it has been several days stuck in moderator approval. It hasn't been posted yet.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
My intuition says:
If you want to run Selenium for Python, you should be using Python code.
If you want to run the script from VBA, write the Python script in Python and make it run from VBA.
If the script should be reading data from an Office document, make your Office document write to an external file that the Python script can also read or have the Python script read the document if sucha thing can be done, which I suppose is possible.

I've had success using Selenium Basic with Chrome. If I remember correctly, it involves downloading the appropriate driver for your current version of Chrome. However, after a certain version of Chrome, you’ll need to switch to using the Chromium browser, as the driver compatibility may no longer be guaranteed with newer Chrome releases, and you would have to refer to Chromium in your code. I did try to use the Firefox version but the documentation is non existing and all it could do is open the window to throw an automation error immediately after.

Nowadays, I do my things with JS and Puppeteer because of personal preference and convenience, but I'd stick to the Chrome webdriver if I wanted to write VBA code. Or use IE :eek:
 
Last edited:
Upvote 0
Thanks Edgar, good food for thought. I suppose for this application I could use Chrome vs. Firefox. From my Google searches, it appears that Selenium may be better supported with Chrome.
I will play around with writing a python script and see how to go about calling it from VBA.
 
Upvote 0
Thanks Edgar, good food for thought. I suppose for this application I could use Chrome vs. Firefox. From my Google searches, it appears that Selenium may be better supported with Chrome.
I will play around with writing a python script and see how to go about calling it from VBA.

I suggested using Python because you downloaded the Python version, but you can write the code in VBA if you're going to use Chrome. The thing with Chrome is that the chromedriver must work with the version of Chrome you have installed on your computer.

Speaking of which, I was curious if I could make the FirefoxDriver work this time after several years and I was able to run it. The method isn't straight forward, though.
I downloaded FF45 from here: PortableApps.com
I installed it in my Downloads folder.
Then, I don't know if it matters, but I placed the geckodriver on the SeleniumBasic path, which is in appdata/local/seleniumbasic
Now, from VBA, I did this:
VBA Code:
Sub testingFF()
    Dim driver As New FirefoxDriver
    driver.SetBinary "C:\Users\myusername\Downloads\ff45portable\FirefoxPortable\firefoxportable.exe"
    driver.Get "https://www.wikipedia.org"
    
    'do stuff
    Stop
    driver.Quit
End Sub

It worked, I used Firefox version 45 because I read it was compatible with that driver. Anyway, I would still use Chrome because my regular and up-to-date installation of Firefox must be closed in order for the portable one to work.

I hope this all makes sense. The point is you can still write VBA code, but the condition is that you must use the right browser.
 
Upvote 0
Hi Edgar et al. Following up with you on this.
I switched to Chrome (v.131.0.6778) Installed Selenium Basic v.2.0.9.0, with Chromedriver v.131.0.6778.108.

Rich (BB code):
Sub SearchWebPage()
    Dim driver As New Selenium.WebDriver
    Dim searchBox As WebElement
    Dim txt_model As String
    Dim keys As New Selenium.keys
   
    txt_model = "Selenium"

    ' Set ChromeDriver path
    driver.SetBinary "C:\Program Files\Google\Chrome\Application\chrome.exe"
    driver.Start "chrome", "https://google.com" ' Replace with your target URL
    driver.Window.Maximize
   
    ' Wait for the page to load
    driver.Wait 5000
   
    ' Find the search box element (update the element selector as needed)
    Set searchBox = driver.FindElementByName("q") ' Replace 'q' with the name attribute of the search box

    ' Enter the search term
    searchBox.SendKeys txt_model
   
    ' Press Enter
    searchBox.SendKeys keys.Enter
   
    ' Wait for results to load
    driver.Wait 5000

    ' Close the browser (optional)
    ' driver.Quit
End Sub

Adding the line in blue got rid of the "unknown error: cannot find Chrome binary" error despite Chrome being installed in the appropriate default location.
Still not able to get it to work. A new error on the line in red ... "unknown error: Runtime.executionContectCreated has invalid 'context' "

I got the Chromedriver based on the comment found here. (If you are using Chrome version 115 or newer, please consult the Chrome for Testing availability dashboard.) But I'm wondering if this is only to be used with the "Chrome for Testing" version (different from the normal version?) also available here - although the version numbers match? Perhaps I can revert back to Chrome version 114 which has the last available specific driver before the "Chrome For testing" of later versions? The "Version Selection" page didn't help much in my opinion. This may be the behaviour encountered with your comment re. the use of Chromium in post #2 Edgar.

Anyone have any thoughts? (this has also been crossposted here hoping to capture the Selenium/Chrome crowd.)
 
Upvote 0
1. Windows key + Run > optionalfeatures > OK
2. Select .NET framework 3.5 if it's not selected > OK
3. Let it install the features > let windows find the files > Restart
4. Download the driver for Win32 or Win64 from Chrome for Testing availability
5. Windows key + Run > appdata > navigate to Local > SeleniumBasic
6. Paste the chromedriver.exe file that came with the download in your SeleniumBasic folder that you just opened, replacing the original, or make a backup first if you want

Try again, with a very simple code like this:
VBA Code:
Sub test()
    Dim driver As New ChromeDriver
    driver.Get "https://wikipedia.org"
    Stop
End Sub

See if the Chrome started and went to the page. If it did, you should be able to use it. I did this with some computer here and it worked. Note: there are many other options available.
 
Upvote 0
Thanks Edgar for your continud support. It appears I don't have .Net framework to select in the optional features list. I looked up .NET framework 3.5 and it appears Windows 11 isn't a supported operating system. So, wondering if your possible solution doesn't apply.

Step #6 ... are you instructing to replace the original chromedriver.exe (shipped with Selenium Basic), with the one downloaded stable 64 bit version from Chrome For Testing? I can do that much, but is this step contingent on having .NET framework accessible?
 
Upvote 0
it appears Windows 11 isn't a supported operating system
The tested machine I talk about in post #6 has Windows 11, the first item of the picture is what I had unticked and then I ticked in order to make the solution work.
1734476675237.png

are you instructing to replace the original chromedriver.exe (shipped with Selenium Basic), with the one downloaded stable 64 bit version from Chrome For Testing? I can do that much, but is this step contingent on having .NET framework accessible?
To get that driver working, you need to enable .NET 3.5, as mentioned earlier. The version of the ChromeDriver included with SeleniumBasic is from 2016 and is incompatible with the latest versions of Chrome.

If you want to use SeleniumBasic, you'll need to figure out how to install that specific version of .NET on your machine or, maybe, downgrade Chrome?

Alternatively, there are other Selenium versions available, such as the Python version, which doesn't require .NET (I suppose). Personally, I use Puppeteer, which allows you to write your scripts in JavaScript and has always been reliable. Additionally, I’ve come across SeleniumVBA, an alternative for VBA users. Although I haven't tested it myself, it seems to have better support.
 
Upvote 0

Forum statistics

Threads
1,224,698
Messages
6,180,424
Members
452,981
Latest member
MarkS1234

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top