Getting a “Type Mismatch” Error in VBA Macro Trying to Scrape Links with Chrome

I created a macro that is meant to go to a webpage from my excel sheet and then scrape all of the links. I am using selenium chrome for this. However, I keep getting a “type mismatch” error no matter what I do. My error is currently coming from the section saying ” For Each element In elements”.

I have tried defining the variable “element” as a string, Object or just leaving it blank. But none of these options work. Is there another variable type that might fix this that I am missing?

Here is the code:

Sub `scraping_web()
    Dim chrm As Selenium.ChromeDriver
    Dim row_no, col_no As Integer
    Dim visitpage
    Dim attributionlink
    Dim fullHTMLlink
    Dim finaloutput
    Dim alttag
    Dim ElementCol As Object
    Dim page As HTMLDocument
    Dim loadmore As WebElement
    Dim loadmoretrue
    Dim anchor As Object
         Url As String
    element

    row_no = 2
    Application.ScreenUpdating = False
    Set chrm = New Selenium.ChromeDriver
    chrm.Start
    Set page = New HTMLDocument
    chrm.Get ("http://website.com")
   For j = 1 To 37

   visitpage = Sheet1.Cells(1, j).Value
    
    Application.Wait DateAdd("s", 10, Now)

    Set page = New HTMLDocument
    chrm.Get (visitpage)
    
  
    Application.Wait DateAdd("s", 10, Now)

     Set element = chrm.FindElementsByTag("a")
    
    ' Loop through the anchor elements and extract the URLs
    For Each element In elements
    Set Url = element.Attribute("href")
        Sheet1.Cells(element, j).Value = Url
        
    Next element



    Next j

End Sub

I have tried I have tried defining the variable “element” as a string, Object or just leaving it blank. But none of these options work.

  • 1

    Try Set elements = chrm.FindElementsByTag("a") not element

    – 

Leave a Comment