繁体   English   中英

使用Excel-VBA登录网站

[英]Logging in to Website with Excel-VBA

感谢您看这个问题。 我正在尝试通过VBA登录网站。 这可能已经被回答了,但是我很难找到可行的方法。 我以前用VBA进行过互联网自动化。 我什至以前都用它来登录过不同的站点,但是这给了我一个问题。 我已经尝试过getElementByID,getElementByTagName等,但是似乎没有什么可以填充“ loginName”和“ password”文本框。

该网站是https://vfo.frontier.com/

我发现奇怪的是,当我要通过GoogleChrome浏览器检查元素时,不会带我去选择的文本框。

非常感谢。 对不起,如果有任何错误,我对VBA还是很陌生,这是我第一次发布该网站。

我现在拥有的代码如下:

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返回元素的集合 -即使只有一个元素,它仍然是一个集合。 因此,您需要正确索引它。 假设此类元素Name的第一个实例是适当的:

UserN(0).Value = "login name"

同样:

PW(0).Value = "my password"

结果:

在此处输入图片说明

使用后期绑定在InternetExplorer.Application (我不知道InternetExplorerMedium是什么...)中进行了测试(尽管应该没关系):

Dim IE as Object
Set IE = CreateObject("InternetExplorer.Application")

下面的方法也可以使用getElementByID方法工作,该方法返回单个元素(因为id是唯一的):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM