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

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,664
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.
 
I think I've solved #2 issue. I declared driver as a public variable. (and removing the Dim in the procedure)
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
The wait method should leave the window open.
driver.wait 30000

It depends what you're waiting for, but I like to use:
VBA Code:
Do While True
    DoEvents
Loop
 
Upvote 0
Yeah, the wait will keep it open for a specified amount of time, but in my case, I need the window to stay open until closed to allow the user to be able to reference it while working from within the VBA application. So, it has to be visible, and can't tie up the application's function. (my next challenge in another post will be how to move the browser window to another part of my screen. Right now Excel obscures it.

I really appreciate you having provided me the solution when even those directly involved with this concept failed. Mr. Excel's members provide best patient support around by far.
 
Upvote 0

Forum statistics

Threads
1,226,516
Messages
6,191,500
Members
453,660
Latest member
Wp1902

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