Excel VBA using Selenium
Asked Answered
F

5

6

I am on Windows 10.
I am trying to use Selenium to automate the Edge browser (or any other browser) via Excel VBA.

I went to https://www.selenium.dev/downloads/

There I downloaded
Selenium Server, Latest stable version 3.141.59
and
Python 3.141.0 November 01, 2018 4.0.0a6.post1 May 28, 2020 Download (I am not using Python but I might in the future.)

Then I opened Excel VBA and opened Tools but could not find the “Selenium Type Library” option in the drop down.

I did more reading and I downloaded SeleniumBasic at https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0

This made the option “Selenium Type Library” appear on the Tools dropdown.

I selected the “Selenium Type Library” option.

I entered this code in the VBA editor, which I got off the web. It opens Chrome. I could not find how to open Edge.

Sub test2()

    Dim driver As New WebDriver
    Dim rowc, cc, columnC As Integer
    driver.Start "Chrome"
    Application.Wait Now + TimeValue("00:00:20")

End Sub

It failed on the line:

driver.Start "Chrome"

It gave an error:

Exception from unknown error

Florencio answered 4/6, 2020 at 22:48 Comment(0)
Z
4

Do you have the selenium chromium driver installed? That is required and it is different than the server and Visual library. This is the actual chrome binary that will launch and run. Download the latest version and place it in path such that basic can access it. I believe the path is C:\Users\ *Username\AppData\Local\SeleniumBasic

The executable should also be lowercase:

Dim driver As New WebDriver 
driver.Start "chrome" 
driver.Get "https://duckduckgo.com" 
Zoezoeller answered 4/6, 2020 at 22:57 Comment(3)
Yes, I have the "chromedriver.exe" installed in C:\Users\myUsername\AppData\Local\SeleniumBasic. Also, In the code line: "driver.Start "Chrome" I changed "Chrome" to "chrome" (all lower case) and it still bombs-out on that line of code.Florencio
Have you deleted the default chromedriver.exe and download the latest copy?Zoezoeller
That was it! I replaced the old chromedriver.exe with the latest driver and, eureka! it worked!Florencio
M
0

don't forget to add the VBA reference: Selenium Type Library

here is a link that had all the puzzle pieces in one place: https://www.makeuseof.com/tag/how-to-automate-firefox-or-chrome-with-vba-and-selenium/

Mendie answered 17/3, 2023 at 16:24 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewMeredithmeredithe
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Vantassel
E
0

The OP did say - to automate the Edge browser (or any other browser) via Excel VBA - and technically nowadays IE is available via Edge in IE mode.

This code works on Windows 11 with Excel 365 VBA.

Sub sbIETest()
      Dim ie As New InternetExplorer ' ref - Microsoft Internet Controls
      ie.Visible = True
      ie.navigate "https://www.microsoft.com/en-gb/download/internet-explorer.aspx"
      Do
        DoEvents
      Loop Until ie.readyState = READYSTATE_COMPLETE
End Sub
Expiry answered 23/10, 2023 at 7:42 Comment(0)
E
0

For Edge -

Check edgedriver.exe version from the command line -

PS C:\Users\david\appdata\Local\SeleniumBasic> .\edgedriver -v

Microsoft Edge WebDriver 116.0.1938.81 (bc0eb28b55bd3...

download the latest edgedriver version -

from here https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

this code was tested on Windows 11 with Excel 365 VBA

Option Explicit

Sub sbBing()
    Dim wd As New WebDriver
    wd.Start "edge", "https://www.bing.com" 
    wd.Get "/" 
    sbDelay (200000) 
End Sub


Sub sbDelay(delay As Long) 
    Dim i As Long 
    For i = 1 To delay 
        DoEvents 
    Next i 
End Sub
Expiry answered 23/10, 2023 at 8:27 Comment(0)
D
-1

For Edge you don't need Selenium. Use "Microsoft Internet Controls" instead of "Selenium Type Libra"

    Sub test()

      Dim ie As New InternetExplorer
      Dim doc As New HTMLDocument
      Dim ecoll As Object
      ie.Visible = True
      ie.navigate "YOUR_URL"
      Do
        DoEvents
      Loop Until ie.readyState = READYSTATE_COMPLETE

    End Sub
Dacoity answered 27/7, 2021 at 10:15 Comment(1)
This is not Edge. This is IE (Internet Explorer) But see: microsoft.com/en-us/download/internet-explorer.aspxDiversity

© 2022 - 2024 — McMap. All rights reserved.