用于打开 Mozilla Firefox 的 VBA 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15635371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:15:04  来源:igfitidea点击:

VBA macro to open Mozilla Firefox

internet-explorervbafirefox

提问by user2165404

Hi I have come up with a code, which will open Internet Explorer, navigate to a website, enter user id and password and finally click the login button.

嗨,我想出了一个代码,它将打开 Internet Explorer,导航到一个网站,输入用户 ID 和密码,最后单击登录按钮。

The code is:

代码是:

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

But as the website for which I have created this script doesn't support Internet Explorer I want to open the same in Firefox. I am clueless and I have not tried anything so far. Please help me out.

但是由于我为其创建此脚本的网站不支持 Internet Explorer,我想在 Firefox 中打开它。我一无所知,到目前为止我还没有尝试过任何东西。请帮帮我。

回答by Ansgar Wiechers

Firefox does not expose a COM object, so it can't be controlled the way IE can be controlled. You may be able to achieve your goal with some other automation tool, though, e.g. Seleniumor AutoIt.

Firefox 不公开 COM 对象,因此无法像控制 IE 那样控制它。不过,您可以使用其他一些自动化工具来实现您的目标,例如SeleniumAutoIt

Another option may be to sniff the authentication traffic (i.e. the communication that takes place when you click the "login" button) with something like Fiddlerand then use VBScript to automate the login with an XMLHTTPRequest:

另一种选择可能是使用Fiddler 之类的东西嗅探身份验证流量(即,当您单击“登录”按钮时发生的通信),然后使用 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&..."