VBA宏打开Mozilla Firefox

时间:2013-03-26 11:00:25

标签: internet-explorer vba firefox

您好我已经提供了一个代码,它将打开Internet Explorer,导航到一个网站,输入用户ID和密码,最后点击登录按钮。

代码是:

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中打开相同的内容。我一无所知,到目前为止我还没有尝试过任何事情。请帮帮我。

1 个答案:

答案 0 :(得分:3)

Firefox不公开COM对象,因此无法控制IE的控制方式。但是,您可以通过其他一些自动化工具实现目标,例如, SeleniumAutoIt

另一种选择可能是使用Fiddler嗅探身份验证流量(即单击“登录”按钮时发生的通信),然后使用VBScript自动登录{{3} }:

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&..."