Scraping if ObjIE.Document.getElementsByClassName.innertext "empty" or not existing returning "text"

doremi

New Member
Joined
Mar 30, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have a list of webpage links on the sheet "memory" in column H and using the macro attached I am getting the price tag from elements ... list_item_price, but if the macro does not find the list_item_price it returns the previous price instead, I would like it to display "it does not exist". The other thing is I would like to first check if the element exist on the webpage at all, and if not look for element with different name, since there are several differently programmed eshops, thus using different names for the price tag. Thank You for your help.



Code:
Sub Scrape1()
    Dim ObjIE As Object
    Dim record As Range
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Worksheets("Memory")
    For Each record In Range(ws.Cells(3, 8), ws.Cells(ws.Cells.SpecialCells(xlCellTypeLastCell).Row, 8))

        Set ObjIE = CreateObject("InternetExplorer.Application")

        ObjIE.Navigate record.Value

        Application.StatusBar = "Loading, Please wait..."

        Do While ObjIE.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop


        Application.StatusBar = "Searching for value. Please wait..."

        Dim dd As String
        On Error Resume Next
        dd = ObjIE.Document.getElementsByClassName("col-md-6 col-sm-6 col-6 list_item_price")(0).innertext
        
        'If ObjIE.Document.getElementsByClassName.Count > 0 Then
        
        If InStr(1, ObjIE.Document.getElementsByClassName.innertext, "col-md-6 col-sm-6 col-6 list_item_price") > 0 Then
            record.Offset(0, 1).Value = dd
        ' Element exists and is not empty
        Else
                record.Offset(0, 1).Value = "Does not exist"
        End If
        
        
        
        Next record
    
    ObjIE.Quit
    
    
    
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
A quick bypass is clearing the variable before trying to fill it:
VBA Code:
        Dim dd As String
        On Error Resume Next
        dd = ""
        dd = ObjIE.Document.getElementsByClassName("col-md-6 col-sm-6 col-6 list_item_price")(0).innertext
When the element is not found then the returned value stays at ""

A more complete solution implies examining the collection:
VBA Code:
        On Error Resume Next
        Set prColl = ObjIE.Document.getElementsByClassName("col-md-6 col-sm-6 col-6 list_item_price")
        On Error GoTo 0
        If prColl Is Nothing Then
            'what to do when the element is not found
            '
        Else
            dd = prColl(0).innertext
            'what to do when the element is there
        End If
Variable prCall should be dimmed "as Object"

Bye
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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