以下是简单IE自动化的代码,只需输入订单号(如1413105088
)和邮政编码(始终为78759
)并点击提交按钮,然后从结果页面获取跟踪编号,如017136295201034
并将它们放在C列中。
它按预期工作,但因为IE不那么可靠和缓慢我想知道是否有更快的方法来执行此过程,如果没有,我可以使它至少可靠,以便它点击提交后不会失败按钮,意思是
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
之后
.document.getElementsByClassName("button_text")(3).Click
失败,因为它没有真正检查ie页面是否已完成加载。
我问这个问题,因为我必须为100次这样的请求做这件事。提前谢谢。
完整代码:
Sub test()
Dim urL As String, orderNum As String
Dim trackingNum, prodDetail, cet
Dim i As Long, fI As Long
Dim IE
urL = "https://fisher-price.mattel.com/webapp/wcs/stores/servlet/OrderStatusGuestView?catalogId=10101&langId=-1&storeId=10151&krypto=prThs8zyeWG0bkF9ajSr%2FCnzmv1TKodtTEw0EdXtC7NjEmfD3cb6Z75umdkcXCiEPFxvkd0TfHkOswm3ZcMp8sbrU2doZFa6TxVbI%2BW1Lzk%3D"
fI = MAIN.Range("B" & Rows.Count).End(xlUp).Row
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
For i = 2 To fI
orderNum = Trim(MAIN.Range("B" & i).Value) 'Sample ordernum = 1413105088
If orderNum <> "" Then
.navigate urL
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
.document.getelementbyid("orderNumber").Value = orderNum
.document.getelementbyid("postalCode").Value = 78759
.document.getElementsByClassName("button_text")(3).Click
Application.Wait Now + TimeValue("00:00:02")
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
prodDetail = .document.getElementsByClassName("productDetails")(0).innerText
If InStr(prodDetail, "Tracking :") > 0 Then
cet = Split(prodDetail, "Tracking :")
trackingNum = Trim(cet(1))
MAIN.Range("C" & i).Value = trackingNum
Else
MAIN.Range("C" & i).Value = "N/A"
End If
End If
Next i
End With
IE.Quit
Set IE = Nothing
End Sub
答案 0 :(得分:1)
即使我遇到了Do While... Loop
未正确加载的问题,所以我使用了下面的代码
x = 0
Do until x = 1
if IsObject(.document.getelementbyid("orderNumber")) Then
.document.getelementbyid("orderNumber").Value = orderNum
.document.getelementbyid("postalCode").Value = 78759
.document.getElementsByClassName("button_text")(3).Click
x = 1
Else
Application.Wait Now + TimeValue("00:00:02")
End if
Loop
<强>工作:强>
由于x=0
它会进入循环,因为找不到IsObject(.document.getelementbyid("orderNumber"))
所以它将等待两秒钟,循环将继续,直到它找到ordernumber,否则它将使值为{{1退出循环。
警告:如果您的代码不起作用,则此代码将运行至永久。您可以为此设置限制循环。