How to wait for the page to load after click

2019-08-20 12:22发布

问题:

Below is the code for simple IE automation which simply inputs order number like 1413105088 and postal code which is always 78759 and clicks on submit button and then from the result page it gets the tracking number like 017136295201034 and puts them in column C.

It works as expected but as IE is not so reliable and slow I was wondering if there is a faster way to do this process and if not can I make it atleast reliable so that it doesn't fails after click on the submit button, meaning the

   Do While IE.Busy Or IE.ReadyState <> 4
        DoEvents
    Loop

after

        .document.getElementsByClassName("button_text")(3).Click

fails as it doesn't really checks if the ie page has finished loading.

I am asking this as I have to do this for 100s of such request. Thanks in advance.

Full code:

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

回答1:

Even i faced this problem where the Do While... Loop did not load properly so i used the below code

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

Working: Since x=0 it will go insde the Loop and since the IsObject(.document.getelementbyid("orderNumber")) was not found so it will wait for two second and loop will continue till it find the ordernumber or else it will make the value as x=1 and exit the loop.

Caution: if your code does not work then this code will run till eternity. For which you can set the limit the loop.