VBA IE表单自动化

时间:2017-01-16 12:05:07

标签: vba webforms

我所知道的很多帖子都非常难以实现这一点。

我创建了登录网站的代码,点击了一个按钮,打开了一个带有表单的新标签,但是我无法在其他帖子中尝试各种示例的情况下将数据输入表单。

我目前的代码是:

''=======================================================
'' Program:   MyQuote
'' Desc:      Writes Data into online web form from data in workbook
'' Called by: Batch File (MyQuote.bat)
'' Call:      MyQuote
'' Info:
    ''This macro has been built improve the time it takes to gain a quotation from the MyQuote website.
    ''Once an agent has completed a quote on the inhouse system, they will run batch file (MyQuote.bat) which FTP's out the quote data, imports it into this
    ''workbook, and uses that data to fill the quote forms on the MyQuote website.
    ''The macro creates a new instance of Internet Explorer, navigates to the site, enters the username and password to login, and navigates through
    ''each question entering the variable extracted from our quote system.
'' Changes----------------------------------------------
'' Date         Programmer       Change
'' 13/01/2017   Craig               Written
''=======================================================
Sub MyQuote()

'Declaring the necessary variables.
    Dim IE              As Object


Dim Bton1 As HTMLGenericElement


'Set browser
        Set IE = CreateObject("InternetExplorer.Application")

'Log Off if already logged in
        IE.navigate "http://www.example.com/logout"

'Wait until IE has loaded
        Do Until IE.readyState = 4
            DoEvents
        Loop


'Navigate to My Quote Website
        IE.navigate "http://www.example.com/"

'Wait until IE has loaded
        Do Until IE.readyState = 4
            DoEvents
        Loop


'Enter values and login to My Quote Website
        IE.document.all.Item("UserName").Value = "user@example.co.uk"
        IE.document.all.Item("Password").Value = "abc"


'Find and press the submit button
        Set objCollection = IE.document.getElementsByTagName("button")


        If objCollection(i).Type = "submit" Then
            Set objElement = objCollection(i)
        End If


        objElement.Click


'Wait while IE re-loads...
        While IE.Busy
            DoEvents
        Wend

'Find and press the New Quote button
        Set objCollection = IE.document.getElementsByTagName("button")

        If objCollection(i).className = "btn btn-success btn-xs" Then
            Set objElement = objCollection(i)
        End If


        objElement.Click


'Wait while IE re-loads...
        While IE.Busy
            DoEvents
        Wend


'Find and press the Agree button on Credit Check pop up overlay
        For Each l In IE.document.getElementsByTagName("button")
            If l.className = "btn btn-success" Then l.Click
            Next

'Wait while IE re-loads...
        While IE.Busy
            DoEvents
        Wend

'Enter Registration
        '=====below is what is not working======
        'IE is now in a new tab with different URL if that helps
        IE.document.all.Item("Registration").Value = "AB12CDE"

        End With




End Sub

我无法提供网站的实际网址/密码等,但下面是我尝试使用的网页和元素的HTML代码段:

                        <input type="text" name="Registration" id="Registration" class="form-control reg-input" data-bind="value: Registration" placeholder="REGISTRATION">
                        <button id="btn-findVehicle" class="btn btn-primary" type="button" data-bind="click: $parent.findVehicle">Find your car </button>

我不知道这是不是因为我正在尝试选择一个对象而我现在处于一个新的IE标签中,但我花了10个小时试图让这个工作没有任何运气。

一旦修复了这一点,我可以继续映射其他40个字段,单选按钮,下拉菜单和&amp;提交按钮(祝我好运)。

任何帮助和建议表示赞赏。

此致

克雷格

1 个答案:

答案 0 :(得分:0)

我建议使用getElementById方法,因为你的id真的很容易找到并且看起来是静态的,你可以在MSDN中查看方法。

我在免费的第三方软件的帮助下自动完成了大量的浏览器工作 - 您可以查看它 - 打开TWEBST。 我只是在导航网页时遇到问题,这需要电子签名。

希望它有所帮助。