使用VBA与网页进行交互

时间:2016-11-18 21:41:05

标签: vba excel-vba internet-explorer excel

我在线发现了一些使用VBA与网页互动的代码,并且已经对其进行了调整。我试图自动登录网站,然后提取一些报告......

这是我到目前为止的代码..

Private Sub IE_Automation()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

' You can uncomment Next line To see form results
IE.Visible = True

' Send the form data To URL As POST binary request
IE.Navigate "https://e-oscar-web.net/EntryController?trigger=Login"

' Statusbar
'Application.StatusBar = "www.e-Oscar.com is loading. Please wait..."

' Wait while IE loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now())
Loop

Set objCollection = IE.document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).ID = "companyId" Then

        ' Set text for search
        objCollection(i).Value = "compId"

    Else
        If objCollection(i).ID = "userId" Then
           objCollection(i).Value = "uId"
    Else
        If objCollection(i).ID = "password" Then
           objCollection(i).Value = "pwd"
    Else
        If objCollection(i).ID = "securityMsgAck1" Then
           objCollection(i).Value = True
    'Else
        'If objCollection(i).Type = "button" Then
           'objCollection(i).Value = True


            ' "Search" button is found
            Set objElement = objCollection(i)
                    End If
                End If
            End If
        End If
    'End If

    i = i + 1
Wend
objElement.Click    ' click button to search

' Wait while IE re-loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

' Show IE
IE.Visible = True

' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

Application.StatusBar = ""
End Sub

这似乎工作正常。它拉起网页,填写三个输入框,然后显示&#34;检查&#34;我已经确认条款和协议的单选按钮,但是当我取消注释时

  'Else
    'If objCollection(i).Type = "button" Then
       'objCollection(i).Value = True

该网页给我一个错误:

  

◾LOGINERROR:安全消息必须由授权人员确认   用户访问e-OSCAR。请再试一次。

就像没有点击单选按钮一样,但我可以清楚地看到它被点击了。我评论说,然后运行它,网页拉起来填写所有3个输入框,并检查单选按钮。当我点击&#34;登录&#34;它会抛出同样的错误。所以必须使用单选按钮本身或我试图点击&#34;点击&#34;它,而不是实际点击按钮的代码&#34;登录&#34;

有没有人对为什么会发生这种情况有任何想法(或建议可以解决这个问题)?   - IE 11   - Excel 2013

0 个答案:

没有答案