David Podolak
David Podolak

Reputation: 195

How do I get VBA to submit a javascript form?

I am not sure if I am referencing the button correctly. I keep on getting:

Run-time error '438': Object doesn't support this property or method.

This is what my code looks like at the moment:

Sub russInd()
    Dim oButton As Object, HTMLdoc As Object
    Dim sht1 As Worksheet, myURL As String
    Set ie = CreateObject("InternetExplorer.Application")
        Set sht1 = Worksheets("Day 1")
        myURL = sht1.Cells(32, 2).Value
        ie.navigate myURL
        ie.Visible = True

        Do Until ie.ReadyState = 4
            DoEvents
        Loop

        'Locate The correct forms and buttons
        Set HTMLdoc = ie.document
        Set oButton = HTMLdoc.querySelectorAll("a[href='javascript:submitForm(document.forms[0].action);']")
        'Check All Checkboxes
        HTMLdoc.getElementByID("chkAll").Click
        oButton.Click
End Sub

The webpage I am using is:

  https://indexcalculator.ftserussell.com/

Here is the webpage code:

Web Page Code

I need to click the next button, I did try using

.getElementByID("CtlNavigation_lblControl")

To click the button, however that just skipped over the command, I guess it clicked nothing. Thanks!

Upvotes: 1

Views: 402

Answers (3)

mplungjan
mplungjan

Reputation: 178421

querySelectorAll returns a nodelist. You likely want querySelector

Try

HTMLdoc.getElementById("CtlNavigation_lblControl").querySelector("a").Click

or

HTMLdoc.querySelector("CtlNavigation_lblControl a").Click

Upvotes: 1

QHarr
QHarr

Reputation: 84475

Use the following selector as it works across all steps. The number of child a elements within the parent with id Ctlnavigation2_lblControl changes so the following is a robust way to always get what you want across pages.

HTMLdoc.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click

Your error, as partially correct in comments, is that you are trying to use a method of certain node types e.g. a tag element on a nodeList (which is what querySelectorAll returns). It does NOT return a collection. That is a very important distinction in VBA. If you try to For Each, as you would with a collection, over that nodeList Excel will crash.

Upvotes: 1

shadow2020
shadow2020

Reputation: 1361

Set your selector to this: (It's saying look for any image in a parent A element.)

Set oButton = HTMLdoc.querySelectorAll("a > img")

Here is the full code with the modification:

Sub russInd()
    Dim oButton As Object, HTMLdoc As Object
    Dim sht1 As Worksheet, myURL As String
    Set ie = CreateObject("InternetExplorer.Application")
        Set sht1 = Worksheets("Day 1")
        myURL = sht1.Cells(32, 2).Value
        ie.navigate myURL
        ie.Visible = True

        Do Until ie.ReadyState = 4
            DoEvents
        Loop

        'Locate The correct forms and buttons
        Set HTMLdoc = ie.document
        Set oButton = HTMLdoc.querySelectorAll("a > img")
        'Check All Checkboxes
        HTMLdoc.getElementByID("chkAll").Click
        oButton.Click
End Sub

Upvotes: 1

Related Questions