How to extract a single span value from a URL behind an iframe Element?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
I am trying to pull a single value from a work website that refreshes every 5 minutes into an Excel cell. This is the final step in automating an entire workbook filled with a bunch of formulas I created. I am using HTTP Get Request to do this. However, I am having problems pulling the actual number I want. It is a span id and it is behind an iFrame element. FlowWidget is the iframe's id. Here is my code I have already. I am new to VBA, so this was a done from a lot of just researching but now I am stuck. Been trying for almost two days now. I have edited it a number of times but with no luck. Now with this one I am getting the error code "object variable or with block variable not set" on the "Set eleNumber" line. Thanks to anyone willing to help. Here is my code.

Sub ScrapInductAmount ()

Dim H As Object, doc As New HTMLDocument, eleNumber As Object

Set H = CreateObject("WinHTTP.WinHTTPRequest.5.1")

URL = "Work URL"

Debug.Print URL
ReTry:

H.SetAutoLogonPolicy 0

H.SetTimeOuts 0, 0, 0, 0

H.Open "GET", URL, False H.send H.WaitForResponse

If H.Status <> 200 Then

MsgBox H.Status & " - " & H.statusText

Exit Sub

End If
Debug.Print H.responseText

doc.Body.innerHTML = H.responseText

Set eleNumber = doc.querySelector("[id='_FlowWidget']").contentDocument.querySelector("span[id=force_induct_count_value]")

Dim inductAmount As String

If Not eleNumber Is Nothing Then inductAmount = eleNumber.innerText

Set doc = Nothing

Range("A1").Value = inductAmount

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome

  • If it is a work site, I suppose you cannot share the URL. Can you post the relevant HTML?
  • I normally do this with Chrome, see post #4 of the thread below. Your method is good but I would need to test it on the actual page.
  • You may need to programmatically enter the frame, see example below.


VBA Code:
Public d As WebDriver

Public Sub Table_n()
Dim col As Object, i%, col2 As Object, j%, s$, te As Object, re As Object, k%, nc%
Const URL As String = "http://intrinsicedge.blogspot.com/"
Set d = New ChromeDriver
With d
    .Start "Chrome"
    .get URL: .Wait 800
    Set col = d.FindElementsByTag("iframe")
    MsgBox col.Count, , "iframe count"
    For i = 1 To col.Count                                          ' loop iframes
        If col.item(i).Attribute("src") Like "*docs.google*" Then
            s = col.item(i).Attribute("src")
            .Quit
            .get s                                                  ' go to docs page
            Set col2 = .FindElementsByTag("iframe")
            .SwitchToFrame col2.item(1)                             ' enter iframe
            Set col2 = .FindElementsByTag("table")
            MsgBox col2.Count, , "table count"
            Set te = col2.item(1).FindElementsByTag("tr")
            Set re = te.item(10).FindElementsByTag("td")
            nc = re.Count
            For j = 2 To col2.item(1).FindElementsByTag("tr").Count - 1
                For k = 1 To nc
                    Set re = te.item(j).FindElementsByTag("td")
                    Cells(j, k) = re.item(k).Text
                    Set re = re.item(k).FindElementsByTag("a")
                    If re.Count > 0 Then ActiveSheet.Hyperlinks.Add Anchor:=Cells(j, k), _
                    Address:=re.item(1).Attribute("href"), TextToDisplay:=CStr(Cells(j, k).Value)
                Next
           Next
           Exit Sub
        End If
    Next
    .Quit
End With
End Sub
 
Upvote 0
Welcome

  • If it is a work site, I suppose you cannot share the URL. Can you post the relevant HTML?
  • I normally do this with Chrome, see post #4 of the thread below. Your method is good but I would need to test it on the actual page.
  • You may need to programmatically enter the frame, see example below.


VBA Code:
Public d As WebDriver

Public Sub Table_n()
Dim col As Object, i%, col2 As Object, j%, s$, te As Object, re As Object, k%, nc%
Const URL As String = "http://intrinsicedge.blogspot.com/"
Set d = New ChromeDriver
With d
    .Start "Chrome"
    .get URL: .Wait 800
    Set col = d.FindElementsByTag("iframe")
    MsgBox col.Count, , "iframe count"
    For i = 1 To col.Count                                          ' loop iframes
        If col.item(i).Attribute("src") Like "*docs.google*" Then
            s = col.item(i).Attribute("src")
            .Quit
            .get s                                                  ' go to docs page
            Set col2 = .FindElementsByTag("iframe")
            .SwitchToFrame col2.item(1)                             ' enter iframe
            Set col2 = .FindElementsByTag("table")
            MsgBox col2.Count, , "table count"
            Set te = col2.item(1).FindElementsByTag("tr")
            Set re = te.item(10).FindElementsByTag("td")
            nc = re.Count
            For j = 2 To col2.item(1).FindElementsByTag("tr").Count - 1
                For k = 1 To nc
                    Set re = te.item(j).FindElementsByTag("td")
                    Cells(j, k) = re.item(k).Text
                    Set re = re.item(k).FindElementsByTag("a")
                    If re.Count > 0 Then ActiveSheet.Hyperlinks.Add Anchor:=Cells(j, k), _
                    Address:=re.item(1).Attribute("href"), TextToDisplay:=CStr(Cells(j, k).Value)
                Next
           Next
           Exit Sub
        End If
    Next
    .Quit
End With
End Sub
Hello. Is there anyway to do this task without downloading something extra. I want to use this task to send the excel file with it around me work so others can use it also.
 
Upvote 0
No, it is what you get when you right click the desired page element and choose inspect:

inspect.PNG
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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