本文介绍了由于网站仍在加载,因此自动输入网站数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码可以从 ThisWorkbook 的多个列中获取数据,并将其放入 Internet Explorer 网站中的各个字段中.点击line1(搜索按钮)后网站加载.然后代码会在点击复选框的 line2 处引发错误,因为如果网站仍在加载,则还没有复选框.(我认为该网站是基于 Sharepoint 构建的,并且编码很差.)

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 line1 (Search button). Then the code throws an error at line2 where it clicks on checkbox, as there is no checkbox yet if the website is still loading. (I think the website is built on Sharepoint and is poorly coded.)

是否有任何代码在 2-3 秒后重复第 2 行并在出现错误时继续执行?我尝试错误处理程序来重复代码,但没有奏效.

Is there any code which repeats line 2 after 2-3 seconds and continues further whenever the error appears? I tried error handler to repeat the code but didn't work.

    Sub CSA_Upload()

        Dim test1 As Long, test2 As Long
        test1 = Timer

        Dim n As Long
        Range("A1").Select
        n = Selection.End(xlDown).Row
        ThisWorkbook.Sheets("Data").Range("A2:A" & n).Interior.ColorIndex = 0

        Dim IE As Object
        Dim doc As Object
        Dim htmlTable As htmlTable
        Set IE = New InternetExplorerMedium
        'Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True

        'Navigate to CSA tool Home Page
        IE.navigate "https://csa.abcdefg.com/Collector_view.aspx/"

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

        Set doc = CreateObject("htmlfile")
        Set doc = IE.document

        'Enter Invoice Number in SearchBy box
        doc.getElementById("ContentPlaceHolder1_ddlSearch").Value = "[Inv Number]"

        Range("A1").Select

        'Count the number of rows in the data list
        Dim X As Long
        Range("A1").Select
        X = Selection.End(xlDown).Row

        'For each invoice number the loop starts here
        For rowNo = 2 To X
            ActiveCell.Offset(1).Select
            'Fill Blue colour in active processing invoice number cell
            ThisWorkbook.Sheets("Data").Range("A" & rowNo).Interior.ColorIndex = 37

            'Input the invoice number
            doc.getElementById("ContentPlaceHolder1_txtSearch").Value = ThisWorkbook.Sheets("Data").Range("A" & rowNo).Value

            'Click the Search button
    'This is the Line1
            doc.getElementById("ContentPlaceHolder1_search").Click

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

            'Checkbox select all
    '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)

            'Enter rest of the data
            doc.getElementById("ContentPlaceHolder1_ddlaction").Value = ThisWorkbook.Sheets("Data").Range("B" & rowNo).Value        'Input Action
            doc.getElementById("ContentPlaceHolder1_txtToDoDate").Value = ThisWorkbook.Sheets("Data").Range("C" & rowNo).Value      'Input Action Date
            doc.getElementById("ContentPlaceHolder1_ddlstatus").Value = ThisWorkbook.Sheets("Data").Range("D" & rowNo).Value        'Input Root Cause
            doc.getElementById("ContentPlaceHolder1_txtcomments").Value = ThisWorkbook.Sheets("Data").Range("E" & rowNo).Value      'Input Comments
            doc.getElementById("ContentPlaceHolder1_btn_Comments").Click                                                            'Click Submit button

            Application.Wait DateAdd("s", 3, Now)

            'Hit enter on MessegeBox
            Application.SendKeys "{ENTER}"

            'Fill Green colour in the active cell when all entries are passed
            ThisWorkbook.Sheets("Data").Range("A" & rowNo).Interior.ColorIndex = 35

        Next 'Proceed to next invoice number

        IE.Quit 'Quit Internet explorer
        test2 = Timer
        MsgBox (X - 1) & " Invoices have been updated and it took " & Format((test2 - test1) / 86400, "hh:mm:ss") & " Seconds."

    End Sub

推荐答案

修订于2019-03-25

我认为错误是因为 doc 被改变了.

重写

' 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)

作为

' 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)

也许有帮助.

这篇关于由于网站仍在加载,因此自动输入网站数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:36