在 excel vba 中获取 html "id" 标签
Getting the html "id" tag in excel vba
我正在研究 UPS 跟踪宏,我基本上使用了以下代码:UPS Automated tracking clicking a button。
但是,在我按照自己的喜好操作代码后,我似乎无法提取发货状态。以下是我所拥有的以及我目前 运行 遇到的错误。
Run-time error '91':
Object variable or With block variable not set
代码:
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://wwwapps.ups.com/WebTracking/track?loc=en_US"
With ie
.Visible = False
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Enter a value in the "Number" text box
ie.document.getElementById("trackNums").Value = "1Z12345E1512345676"
' Click the "Submit" button
Set Results = ie.document.getElementsByTagName("input")
For Each itm In Results
If InStr(1, itm.outerhtml, "Track", vbTextCompare) > 0 Then
itm.Click
Exit For
End If
Next
' Click the "Shipment Progress" button
Set Results = ie.document.getElementsByTagName("input")
For Each itm In Results
If InStr(1, itm.outerhtml, "Shipment Progress", vbTextCompare) > 0 Then
itm.Click
Exit For
End If
Next
' Get the text from the "Shipment Progress Table" and assign to a variable
Dim status As String
Set doc = ie.document
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
'tbltxt = doc.getElementsByTagName("pkgstep1").innerText
status = doc.getElementById("tt_spStatus").innerText
' process the text as desired
ActiveCell.Value = status
End Sub
我 运行 遇到问题的确切行是:
status = doc.getElementById("tt_spStatus").innerText
这是我要从标签中提取的 text
的屏幕截图。
错误行上方的几行,你有这一行:
Do Until ie.readyState = 4
改成这样:
Do While ie.Busy Or ie.readyState <> 4
我正在研究 UPS 跟踪宏,我基本上使用了以下代码:UPS Automated tracking clicking a button。
但是,在我按照自己的喜好操作代码后,我似乎无法提取发货状态。以下是我所拥有的以及我目前 运行 遇到的错误。
Run-time error '91': Object variable or With block variable not set
代码:
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://wwwapps.ups.com/WebTracking/track?loc=en_US"
With ie
.Visible = False
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Enter a value in the "Number" text box
ie.document.getElementById("trackNums").Value = "1Z12345E1512345676"
' Click the "Submit" button
Set Results = ie.document.getElementsByTagName("input")
For Each itm In Results
If InStr(1, itm.outerhtml, "Track", vbTextCompare) > 0 Then
itm.Click
Exit For
End If
Next
' Click the "Shipment Progress" button
Set Results = ie.document.getElementsByTagName("input")
For Each itm In Results
If InStr(1, itm.outerhtml, "Shipment Progress", vbTextCompare) > 0 Then
itm.Click
Exit For
End If
Next
' Get the text from the "Shipment Progress Table" and assign to a variable
Dim status As String
Set doc = ie.document
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
'tbltxt = doc.getElementsByTagName("pkgstep1").innerText
status = doc.getElementById("tt_spStatus").innerText
' process the text as desired
ActiveCell.Value = status
End Sub
我 运行 遇到问题的确切行是:
status = doc.getElementById("tt_spStatus").innerText
这是我要从标签中提取的 text
的屏幕截图。
错误行上方的几行,你有这一行:
Do Until ie.readyState = 4
改成这样:
Do While ie.Busy Or ie.readyState <> 4