单击后如何等待页面加载 [英] How to wait for the page to load after click

查看:77
本文介绍了单击后如何等待页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是用于简单IE自动化的代码,该代码只需输入订单号(如 1413105088 )和邮政编码(始终为 78759 ),然后单击提交"按钮,然后从结果页面中单击它会获得跟踪号,例如 017136295201034 并将它们放在C列中.

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.

它可以按预期工作,但由于IE不太可靠且运行缓慢,我想知道是否有更快的方法来执行此过程,如果不能,我可以使其至少可靠,以便在单击提交后它不会失败按钮,即

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

之后

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

失败,因为它没有真正检查ie页面是否已完成加载.

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

我要这个,因为我必须在100个这样的请求中执行此操作.预先感谢.

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

完整代码:

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

推荐答案

即使我遇到了 Do While ... Loop 无法正确加载的问题,所以我使用了以下代码

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

工作:由于 x = 0 它将进入循环,并且由于未找到 IsObject(.document.getelementbyid("orderNumber")),因此它将等待两秒钟,然后循环将继续直到找到订单号,否则它将使值成为 x = 1 并退出循环.

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.

警告:如果您的代码不起作用,那么该代码将一直运行到永恒.您可以为其设置极限循环.

这篇关于单击后如何等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆