运行时错误'424':对象必需IE.Document.GetElementById

时间:2019-04-03 11:58:51

标签: excel vba web-scraping

我正在尝试使用Excel文件的值自动完成网站上的表单。

Sub CommandButton1_Click()
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://www.ryanair.com/be/nl/check-in"
End With

Do Until IE.readyState = 4
DoEvents
Loop

   IE.document.getElementbyid("username").Value = "resa@connections.be"


End Sub

2 个答案:

答案 0 :(得分:0)

使用定时循环显示元素

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
'
Public Sub CommandButton1_Click()
    Dim ie As New InternetExplorer, t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 10


    With ie
        .Visible = True
        .Navigate2 "https://www.ryanair.com/be/nl/check-in"

        While .Busy Or .readyState < 4: DoEvents: Wend
        t = Timer
        Do
            On Error Resume Next
            Set ele = .document.getElementById("username")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        If ele Is Nothing Then Exit Sub

        ele.Value = "resa@connections.be"

        Stop
        .Quit
    End With
End Sub

答案 1 :(得分:0)

感谢您的快速解答!我收到了一个错误并使用按钮进行了一些小的更改。现在它就像魅力一样!

Public Sub CommandButton1_Click()
    Dim ie As New InternetExplorer, t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 10
    t = Timer

    With ie
        .Visible = True
        .Navigate2 "https://www.ryanair.com/be/nl/check-in"

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

        Do
            On Error Resume Next
            Set ele = .document.getElementById("username")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        If ele Is Nothing Then Exit Sub

        ele.Value = "resa@connections.be"
End With
End Sub