Search code examples
excelvbainternet-explorergetelementsbyname

Why isn't "get Elements By Name" working?


I'm pretty new to programming. Can someone help me with this? It always crashes at the getElementsByName line, can't work out why..

Option Explicit
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems


Sub getVerb()
    Dim IE As Object ', objShellWindows As Object
    Dim verb As String, strWebPath As String

    strWebPath = "http://www.conjugation.org/"

    verb = "querer"

    'Navigate to page
    '----------------
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate strWebPath
    End With

    'Wait for page
    Do While IE.Busy
        Sleep 250
        DoEvents
    Loop

    'Fill out
    '---------
    'Enter verb
    '<input type="text" size="25" name="word">
    IE.document.getElementsByName("word")(0).Value = verb

    'Set to List
    '<input type="radio" name="rb1" value="list">
    IE.document.getElementsByName("rb1")(0).Value = "list"

    'Press Button Conjugate
    '<input type="submit" name="B1" value="Conjugate">
    IE.document.getElementByName("B1").Click

    'TODO: extract info

    'Exit IE
    '--------
    IE.Quit
    Set IE = Nothing

End Sub

Solution

  • This is an optimized script that makes use of proper page load wait and then css selectors. CSS selectors are a faster, more flexible, way of matching on elements. I think it makes for nice clean reading as well.

    The [x=y] e.g. [value=list] are attribute = value selectors. The input is a type selector. These selectors are applied via querySelector method of HTMLDocument object and return the first match in the DOM for the specified css selector.

    Option Explicit
    
    'VBE > Tools > References:
    ' Microsoft Internet Controls
    Public Sub EnterInfo()
        Dim ie As New InternetExplorer
        Const VERB As String = "querer"
    
        With ie
            .Visible = True
            .Navigate2 "http://www.conjugation.org/"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            With .document
                .querySelector("input").Value = VERB 'first input tag element
                .querySelector("[value=list]").Click '<  first element with value attribute having value of list
                .querySelector("[value=Conjugate]").Click '<  first element with value attribute having value of Conjugate
            End With
    
            Stop '<= delete me later
            .Quit
        End With
    End Sub
    

    Exploring css selectors in your browser (Chrome shown):

    enter image description here


    Practicing css selectors:

    https://flukeout.github.io/