网站搜索失败时如何处理运行时错误424? [英] How to handle run time error 424 when website search fails?
问题描述
我从IE中的跟踪快递网站跟踪寄售编号.
I track consignment numbers from tracking courier website in IE.
一些寄售编号没有详细信息,它会从中抛出消息
A few consignment numbers do not have details from which it throws out a message
无效的AWB号.请稍后重试."
"Invalid AWB No. Please try again later."
当我的代码由于给出运行时错误424而停止执行时.
When my code stops execution by giving run time error 424.
如何跳过并转到下一项搜索?
How do I skip and move to next item search?
Sub CountryPopList()
'declare the variables
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim oSearch As HTMLDivElement
Dim i As Integer
'create and get access to an instance of IE
Set ieObj = New InternetExplorer
LastRow = Range("A" & Rows.Count).End(xlUp).Row
With ieObj
.Visible = True
.navigate "https://shipway.in/rivigo_courier"
Do While .Busy Or .readyState <> 4: DoEvents: Loop
End With
For i = 2 To LastRow
'in the search box put cell "A2" value
ieObj.document.getElementById("waybill").Value = Sheets("Sheet1").Range("A" & i).Value
'click the 'go' button
ieObj.document.getElementsByName("submit")(0).Click
With ActiveSheet
.Range("A" & i).Value = ieObj.document.getElementsByClassName("shp_table")(0).getElementsByTagName("tr")(1).Children(1).textContent
.Range("B" & i).Value = ieObj.document.getElementsByClassName("shp_table")(0).getElementsByTagName("tr")(2).Children(1).textContent
.Range("C" & i).Value = ieObj.document.getElementsByClassName("shp_table")(0).getElementsByTagName("tr")(5).Children(1).textContent
End With
Next i
End Sub
推荐答案
如果您提供有效的代码进行测试,我将对此进行加强.您可以使用定时循环来等待结果表出现(我实际上是在等待第一个td单元格,因此我可以重新使用稍后持有的变量).然后检查第一个表格单元格是否包含文本 Invalid AWB
.仅当不存在时,才执行If语句中的代码行.
If you provide a valid code to test with I will tighten this up. You can use a timed loop to wait for the results table to be present (I in fact wait for the first td cell so I can re-use the variable I hold this in later). Then check if the first table cell contains the text Invalid AWB
. Only if that is not present execute the lines of code within the If statement.
通常,我最初会从工作表中读取所有搜索值,然后将其读取到数组中,然后循环执行数组并执行搜索.将结果存储在一个数组中,并最后一次写完该数组.
Generally, I would read all search values in from sheet initially, into an array, then loop the array and perform your searches. Store results in an array and write that array out in one go at end.
此外,我将使用显式工作表名称代替 Activesheet
.我不知道您是否打算覆盖 Sheet1
,但这在使用 Activesheet
时是有可能的;如果 Sheet1
当前处于活动状态.
Furthermore, I would use an explicit sheet name in place of Activesheet
. I don't know whether you intend to overwrite Sheet1
, but it is a possibility when using Activesheet
; if Sheet1
is currently active.
我已经使用 querySelector 重新-编写一些元素选择.此方法应用 css选择器来匹配元素.它更快,更灵活.同样,使用有效的搜索值,我可以测试并确保索引和语法正确.您始终可以在该部分中替换原始代码行.
I've used querySelector to re-write some of your element selection. This method applies css selectors to match on elements. It is faster and more flexible. Again, with a valid search value I can test and ensure indexing and syntax is correct. You can always substitute your original code lines back in that section.
Option Explicit
Public Sub CountryPopList()
Dim ieObj As InternetExplorer, htmlEle As IHTMLElement, t As Date, td As IHTMLTableCell
Dim oSearch As HTMLDivElement, i As Long, lastRow As Long, ws As Worksheet
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set ieObj = New InternetExplorer
lastRow = Range("A" & Rows.Count).End(xlUp).Row
With ieObj
.Visible = True
.navigate "https://shipway.in/rivigo_courier"
Do While .Busy Or .readyState <> 4: DoEvents: Loop
For i = 2 To lastRow
.document.getElementById("waybill").Value = ws.Range("A" & i).Value
.document.querySelector("[name=submit]").Click
t = Timer
Do
On Error Resume Next
Set td = .document.querySelector(".shp_table td")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While td Is Nothing
If Not td Is Nothing Then
If Not InStr(td.innerText, "Invalid AWB") > 0 Then
With ActiveSheet '<= should probably use an explicit sheet here or could overwrite Sheet1. Also, bug prone.
.Range("A" & i).Value = .document.querySelector(".shp_table tr:nth-of-type(1) td").textContent 'verify correct when provided working search code
.Range("B" & i).Value = .document.querySelector(".shp_table tr:nth-of-type(2) td").textContent
.Range("C" & i).Value = .document.querySelector(".shp_table tr:nth-of-type(5) td").textContent
End With
End If
End If
Set td = Nothing
Next i
.Quit
End With
End Sub
这篇关于网站搜索失败时如何处理运行时错误424?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!