简体   繁体   English

为什么“按名称获取元素”不起作用?

[英]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..它总是在 getElementsByName 行崩溃,不知道为什么..

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

This is an optimized script that makes use of proper page load wait and then css selectors.这是一个优化的脚本,它利用适当的页面加载等待,然后是 css 选择器。 CSS selectors are a faster, more flexible, way of matching on elements. CSS 选择器是一种更快、更灵活的元素匹配方式。 I think it makes for nice clean reading as well.我认为它也可以使阅读变得干净利落。

The [x=y] eg [value=list] are attribute = value selectors . [x=y]例如[value=list]attribute = value selectors The input is atype selector . input是一个type选择器 These selectors are applied via querySelector method of HTMLDocument object and return the first match in the DOM for the specified css selector.这些选择器通过HTMLDocument对象的querySelector方法应用,并为指定的 css 选择器返回 DOM 中的第一个匹配项。

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):在浏览器中探索 css 选择器(显示为 Chrome):

在此处输入图片说明


Practicing css selectors:练习 css 选择器:

https://flukeout.github.io/ https://flukeout.github.io/

When I'm working with a hierarchical data structure such as HTML (or JSON for that matter) I find myself getting easily confused by assuming which data element I'm working on.当我使用诸如 HTML(或 JSON)之类的分层数据结构时,我发现自己很容易因为假设我正在处理哪个数据元素而感到困惑。 This is especially true if I start stacking up multi-level references as you've done (and I've seen and done much deeper levels that this).如果我像您所做的那样开始堆叠多级引用(并且我已经看到并完成了更深层次的操作),则尤其如此。

So I most often turn on early binding: in this case go to Tools-->References and make sure the Microsoft HTML Object Library is checked.所以我最常打开早期绑定:在这种情况下,转到工具-->参考并确保选中Microsoft HTML 对象库

Then, I unpack the levels into intermediate objects:然后,我将关卡解压为中间对象:

Dim nodeList As IHTMLElementCollection
Set nodeList = IE.document.getElementsByName("word")
nodeList.Item(0).Value = verb

This way I can use the VBE debugger and examine the data structures and build my confidence that I'm working with the exact data element and list item (and sub-element, etc) that I wanted.通过这种方式,我可以使用 VBE 调试器并检查数据结构并建立我的信心,即我正在使用我想要的确切数据元素和列表项(以及子元素等)。

I was able to make the following changes to your code and make it work for me:我能够对您的代码进行以下更改并使其对我有用:

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">
    Dim nodeList As IHTMLElementCollection
    Set nodeList = IE.document.getElementsByName("word")
    nodeList.Item(0).Value = verb

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

    'Press Button Conjugate
    '<input type="submit" name="B1" value="Conjugate">
    Set nodeList = IE.document.getElementsByName("B1")
    nodeList.Item(0).Click

    'TODO: extract info

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

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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