VBA宏打开Mozilla Firefox [英] VBA macro to open Mozilla Firefox
问题描述
您好我已经想出了一个代码,它将打开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
使用objIE
。导航https://website.co.in主页$ b $ b。可见= 1
Do .READYSTATE<> 4:DoEvents:Loop
Application.Wait(Now + TimeValue(0:00:02))
设置htmlDoc = .document
设置htmlColl = htmlDoc.getElementsByTagName INPUT)
Do而htmlDoc.READYSTATE<> complete:DoEvents:Loop
对于每个htmlInput在htmlColl中
如果htmlInput.Name =UserName或htmlInput.Type =text则
htmlInput.Value =Adidas
else
如果htmlInput.Name =password那么
htmlInput.Value =Daddy123
End If
End If
Next htmlInput
设置htmlDoc = .document
设置htmlColl = htmlDoc.getElementsByTagName(input)
Do当htmlDoc.READYSTATE<> complete:DoEvents:Loop
对于每个htmlInput在htmlColl中
如果Trim(htmlInput.Type)=submit则
htmlInput.Click
退出
End if
Next htmlInput
End With
End Sub
我已经创建了这个脚本的网站不支持Internet Explorer我想在Firefox中打开它。我很无知,迄今为止我还没有尝试过任何东西。请帮我解决。 解决方案
Firefox不公开一个COM对象,所以IE不能控制IE的方式受控。尽管如此,您也可以使用其他自动化工具来实现您的目标。 Selenium 或 AutoIt 。
另一个选项可能是嗅探身份验证流量(即单击登录按钮时发生的通信)像 Fiddler ,然后使用VBScript通过 XMLHTTPRequest :
Set req = CreateObject(MSXML2.XMLHTTP.6.0)
pre>
req.openPOST,http://www.example.org/,False
req.setRequestHeader Content-Type,application / x-www-form-urlencoded
req.sendfield1 = foo& field2 = bar& ...
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.
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.
解决方案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. Selenium or AutoIt.
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 Fiddler and then use VBScript to automate the login with an 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宏打开Mozilla Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!