簡體   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