简体   繁体   中英

Excel VBA - XMLHTTP: How to refer to button with no ID, only name

I have been attempting to log in to a website using Excel VBA

The user name and password fields work fine

It's the button to click that's giving me issues

The button does not have an 'ID' rather a name

See Image: 图片

See highlighted line

Dim siteDoc As Object
Set siteDoc = appIE.document

   siteDoc.getElementById("xName").Value = "user"
   siteDoc.getElementById("xPassword").Value = "password"
   siteDoc.getElementById("loginbutton").Click

I receive my Run-time 424 Object Required error on: siteDoc.getElementById("loginbutton").Click

Edit: In the inspect element 'Console' it returns a node list

Edit:

What worked:

Set buttonObject = siteDoc.getElementsByName("loginbutton")
For Each btnObj In buttonObject
    btnObj.Click
Next btnObj

You could find the collection of buttons (class names "button") and loop through until you get the name attribute "loginbutton"

  1. Assign IHTMLElementCollection ( Set Buttons= doc.getElementsByClassName("button") )

  2. Loop through Buttons object to find the login button - your whole code would look like this:

    Sub Example()

    Set Buttons = doc.getElementsByClassName("button")

    For Each oElement In Buttons If oElement.getAttribute("name") = "loginbutton" Then ' this oElement is you button so you action goes below here '........ End If Next

    End Sub

Declare oElement and Buttons as objects

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