Clicking buttons in IE with VBScript on CRA website

与世无争的帅哥 提交于 2019-12-12 05:16:42

问题


I’m very new to VBScript and I’m trying to automate some payroll calculations through the CRA website http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html in excel. I’m been trying to figure this out for few days now with no luck. I think my problem is very simple: I’m trying to click on the “I accept button” at the bottom of the page of the website above.

The code I’ve written is below but for the following line I don’t know how to actually click the button. I always get an error here whatever I try.

 objIE.Application.document.getElementById("??????").Click

The HTML code below has no ID. I’ve also tried various other things like getElementByType, getElementByValue with no luck

VBScript

Sub test()

Set objIE = CreateObject("InternetExplorer.Application")

objIE.Application.Visible = True
objIE.Application.navigate "http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html"

Do While objIE.Application.Busy Or _
    objIE.Application.readyState <> 4
    DoEvents
Loop

objIE.Application.document.getElementById("??????").Click

Do While objIE.Application.Busy Or _
    objIE.Application.readyState <> 4
    DoEvents
Loop

End Sub

HTML Code

<div class="alignCenter">
<input type="button" title="I accept - Begin the calculation" value="I accept" onclick="parent.location='https://apps.cra-arc.gc.ca/ebci/rhpd/startLanguage.do?lang=English'" />
<input type="button" title="I do not accept - Return to Payroll" value="I do not accept" onclick="parent.location='/tx/bsnss/tpcs/pyrll/menu-eng.html'" />
</div>

I would appreciate someones help.

Thanks,

Matthew


回答1:


Note that VB/VBA DoEvents function not available in VBScript. However, here is working code.

Call test

Sub test()

    With CreateObject("InternetExplorer.Application")

        .Visible = True
        .Navigate "http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html"

        Do While .Busy Or .readyState <> 4
            WScript.Sleep 50
        Loop

        Set oInputs = .Document.getElementsByTagName("input")

        For Each elm In oInputs
            If elm.Value = "I accept" Then
                elm.Click
                Exit For
            End If
        Next

        Do While .Busy Or .readyState <> 4
            WScript.Sleep 50
        Loop

        End With

End Sub


来源:https://stackoverflow.com/questions/14817436/clicking-buttons-in-ie-with-vbscript-on-cra-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!