使用Excel-VBA登录网站 [英] Logging in to Website with Excel-VBA
问题描述
感谢您看看这个问题。我正尝试通过VBA登录到网站。这可能已经得到解答,但我无法找到有用的东西。我以前用VBA做过互联网自动化。我甚至用它来登录到不同的网站,但这是一个问题。我已经尝试过getElementByID,getElementByTagName等,但似乎无法填充loginName和password文本框。
我的发现奇怪的是,当我通过GoogleChrome浏览器去检查一个元素时,它不会将我带到我选择的文本框。
非常感谢。对不起,任何错误,我仍然是VBA的新手,这是我第一次发布到网站。
现在的代码如下:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ .IHTMLElement
Dim PW As Object'MSHTML.IHTMLElement
Set IE = New InternetExplorerMedium
uRl =https://vfo.frontier.com/
使用IE
.navigate uRl
.Visible = True
结束使用
'循环直到页面完成加载
Do而IE.Busy
Loop
'在文本框中输入用户名和密码
设置UserN = IE.document.getElementsByName(loginName)
如果Not UserN是Nothing然后
'填入名为username的第一个元素,假定为登录名字段
UserN.Value =登录名称
结束如果
Set PW = IE.document .getElementsByName(password)
'password
如果不是PW不是那么
'填入第一个名为password的元素,假设为密码字段
PW.Value =my Password
End If
End Sub
UserN(0).Value =login name
同样:
PW(0).Value =我的密码
结果:
在 InternetExplorer.Application
(我不知道InternetExplorerMedium是什么...)中使用后期绑定(尽管这不重要)进行测试:
将IE变为Object
Set IE = CreateObject(InternetExplorer.Application)
以下也可以使用 getElementByID
方法,因为 id
是唯一的):
设置UserN = IE.document.getElementByID(loginName)
如果Not UserN不是Nothing然后
'先填入名为username的元素,假定为登录名字段
UserN.Value =blargh
End If
Set PW = IE.document.getElementByID(password)
'password
If Not PW Is Nothing然后
'填写名为password的第一个元素,假定为密码字段
PW.Value =my Password
End如果
Thanks for taking a look at the question. I am trying to log into a website through VBA. This may have already been answered, but I'm having trouble finding something that works. I've done internet automation with VBA before. I've even used it to log into different sites before, but this one is giving me an issue. I've tried getElementByID, getElementByTagName, etc, but nothing seems to be able to populate the "loginName" and "password" text boxes.
The website is https://vfo.frontier.com/
What I found odd is that when I would go to inspect an element through GoogleChrome Browser it wouldn't take me to the text box I had selected.
Thanks so much in advance. Sorry for any errors, I'm still pretty new to VBA and this is my first post to the site.
The code I have now is below:
Sub FVFO()
Dim IE As InternetExplorerMedium
Dim uRl As String
Dim UserN As Object 'MSHTML.IHTMLElement
Dim PW As Object 'MSHTML.IHTMLElement
Set IE = New InternetExplorerMedium
uRl = "https://vfo.frontier.com/"
With IE
.navigate uRl
.Visible = True
End With
' loop until the page finishes loading
Do While IE.Busy
Loop
' enter username and password in textboxes
Set UserN = IE.document.getElementsByName("loginName")
If Not UserN Is Nothing Then
' fill in first element named "username", assumed to be the login name field
UserN.Value = "login name"
End If
Set PW = IE.document.getElementsByName("password")
' password
If Not PW Is Nothing Then
' fill in first element named "password", assumed to be the password field
PW.Value = "my Password"
End If
End Sub
GetElementsByName
returns a collection of elements -- even if there is only one element, it is still a collection. So, you need to index it correctly. Assuming the first instance of such an element Name is the appropriate one:
UserN(0).Value = "login name"
And likewise:
PW(0).Value = "my password"
Results:
Tested in InternetExplorer.Application
(I don't know what InternetExplorerMedium is...) using late-binding (although that should not matter):
Dim IE as Object
Set IE = CreateObject("InternetExplorer.Application")
The following also works using the getElementByID
method, which returns a single element (because the id
is unique):
Set UserN = IE.document.getElementByID("loginName")
If Not UserN Is Nothing Then
' fill in first element named "username", assumed to be the login name field
UserN.Value = "blargh"
End If
Set PW = IE.document.getElementByID("password")
' password
If Not PW Is Nothing Then
' fill in first element named "password", assumed to be the password field
PW.Value = "my Password"
End If
这篇关于使用Excel-VBA登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!