简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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