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

问题描述 投票:1回答:2

我正在尝试使用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 = "[email protected]"


End Sub
excel vba web-scraping
2个回答
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 = "[email protected]"
End With
End Sub

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 = "[email protected]"

        Stop
        .Quit
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.