Error automating website data entry as the website is still loading

后端 未结 3 1821
悲&欢浪女
悲&欢浪女 2021-01-25 09:49

I have code which picks up data from multiple columns from ThisWorkbook and puts in various field in website in internet explorer. The website loads after clicking on <

相关标签:
3条回答
  • 2021-01-25 10:15

    revised on 2019-03-25

    I think the error is thrown because the doc is changed.

    Rewrite

    ' This is the Line2
        doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
        'Wait 3 seconds till it selects all the checkboxes
        Application.Wait DateAdd("s", 3, Now)
    

    as

    ' This is the Line2 
        application.wait Application.Wait DateAdd("s", 1, Now)
        set doc = IE.document
        doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
        'Wait 3 seconds till it selects all the checkboxes
        Application.Wait DateAdd("s", 3, Now)
    

    maybe helpful.

    0 讨论(0)
  • 2021-01-25 10:20

    Use proper page load waits after each .Navigate and .Click.

    While ie.Busy Or ie.readyState < 4: DoEvents: Wend
    

    Also, you can wrap elements that are throwing errors, related to timings, in timed loops which attempt to set the object reference

    Dim t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 10
    
    t = Timer
    Do
        On Error Resume Next
        Set ele = doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll")
        On Error GoTo 0
        If Timer - t > MAX_WAIT_SEC Then Exit Do
    Loop While ele Is Nothing
    
    If Not ele Is Nothing Then
        ele.Click
    End If
    
    0 讨论(0)
  • 2021-01-25 10:31

    I have removed below wait loop after line 1.

        'Wait till it loads
        Do While IE.Busy
            Application.Wait DateAdd("s", 5, Now)
        Loop
    

    and added fix 10 seconds wait time Application.Wait DateAdd("s", 10, Now) just before

        doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
        'Wait 3 seconds till it selects all the checkboxes
        Application.Wait DateAdd("s", 3, Now)
    

    So the final piece of code is as below and it's working!

    'This is the Line1
            doc.getElementById("ContentPlaceHolder1_search").Click
    
            'Checkbox select all
    'This is the Line2
            Application.Wait DateAdd("s", 10, Now)
            doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
            'Wait 3 seconds till it selects all the checkboxes
            Application.Wait DateAdd("s", 3, Now)
    
    0 讨论(0)
提交回复
热议问题