VBA宏打开Mozilla Firefox浏览器(VBA macro to open Mozilla

2019-10-17 21:23发布

您好我想出了一个代码,这将打开Internet Explorer,浏览到一个网站,输入用户名和密码,最后点击登录按钮。

该代码是:

Public Sub LOGIN()

    Dim objIE As SHDocVw.InternetExplorer 
    Dim htmlDoc As MSHTML.HTMLDocument 
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlColl As MSHTML.IHTMLElementCollection

    Set objIE = New SHDocVw.InternetExplorer

    With objIE
        .Navigate "https://website.co.in" ' Main page
        .Visible = 1
        Do While .READYSTATE <> 4: DoEvents: Loop
        Application.Wait (Now + TimeValue("0:00:02"))

        Set htmlDoc = .document
        Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
        Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
        For Each htmlInput In htmlColl
            If htmlInput.Name = "UserName" Or htmlInput.Type = "text" Then
                htmlInput.Value = "Adidas"
            Else
               If htmlInput.Name = "password" Then
                 htmlInput.Value = "Daddy123"

                End If
            End If
        Next htmlInput

        Set htmlDoc = .document
        Set htmlColl = htmlDoc.getElementsByTagName("input")
        Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
        For Each htmlInput In htmlColl
            If Trim(htmlInput.Type) = "submit" Then
                htmlInput.Click
                Exit For
            End If
        Next htmlInput
    End With
End Sub

至于我创造了这个脚本不支持Internet Explorer中的网站,我想在Firefox中打开一样。 我无言以对,我还没有尝试过这么远。 请帮助我。

Answer 1:

Firefox不暴露一个COM对象,所以它不能被控制IE可以被控制的方式。 您可以与其他一些自动化工具来实现自己的目标,不过,如硒或AutoIt的 。

另一种选择可能是嗅出认证业务(即所发生的通信,当你点击“登录”按钮)的东西,如提琴手 ,然后使用VBScript来登录与自动化XMLHTTPRequest的 :

Set req = CreateObject("MSXML2.XMLHTTP.6.0")
req.open "POST", "http://www.example.org/", False
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send "field1=foo&field2=bar&..."


文章来源: VBA macro to open Mozilla Firefox