繁体   English   中英

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

[英]Why isn't "get Elements By Name" working?

我对编程很陌生。 有人可以帮我弄这个吗? 它总是在 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

这是一个优化的脚本,它利用适当的页面加载等待,然后是 css 选择器。 CSS 选择器是一种更快、更灵活的元素匹配方式。 我认为它也可以使阅读变得干净利落。

[x=y]例如[value=list]attribute = value selectors input是一个type选择器 这些选择器通过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

在浏览器中探索 css 选择器(显示为 Chrome):

在此处输入图片说明


练习 css 选择器:

https://flukeout.github.io/

当我使用诸如 HTML(或 JSON)之类的分层数据结构时,我发现自己很容易因为假设我正在处理哪个数据元素而感到困惑。 如果我像您所做的那样开始堆叠多级引用(并且我已经看到并完成了更深层次的操作),则尤其如此。

所以我最常打开早期绑定:在这种情况下,转到工具-->参考并确保选中Microsoft HTML 对象库

然后,我将关卡解压为中间对象:

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

通过这种方式,我可以使用 VBE 调试器并检查数据结构并建立我的信心,即我正在使用我想要的确切数据元素和列表项(以及子元素等)。

我能够对您的代码进行以下更改并使其对我有用:

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