简体   繁体   English

使用Excel-VBA登录网站

[英]Logging in to Website with Excel-VBA

Thanks for taking a look at the question. 感谢您看这个问题。 I am trying to log into a website through VBA. 我正在尝试通过VBA登录网站。 This may have already been answered, but I'm having trouble finding something that works. 这可能已经被回答了,但是我很难找到可行的方法。 I've done internet automation with VBA before. 我以前用VBA进行过互联网自动化。 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. 我已经尝试过getElementByID,getElementByTagName等,但是似乎没有什么可以填充“ loginName”和“ password”文本框。

The website is https://vfo.frontier.com/ 该网站是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. 我发现奇怪的是,当我要通过GoogleChrome浏览器检查元素时,不会带我去选择的文本框。

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. 对不起,如果有任何错误,我对VBA还是很陌生,这是我第一次发布该网站。

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. GetElementsByName返回元素的集合 -即使只有一个元素,它仍然是一个集合。 So, you need to index it correctly. 因此,您需要正确索引它。 Assuming the first instance of such an element Name is the appropriate one: 假设此类元素Name的第一个实例是适当的:

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

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): 下面的方法也可以使用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