【问题标题】:getting error in setting object variable IE.Document.getElement设置对象变量 IE.Document.getElement 时出错
【发布时间】:2016-08-31 00:07:31
【问题描述】:

我试图通过给出关键字“AABDG27”从网页中获取一些数据,然后单击搜索按钮,最后收集搜索结果的最后一项,即市场数据类别 = ESZ。这是一个开放的网站,因此可以测试以下代码。下面是我的代码。它在 Set mydata = IE.Document.getElements.... 行中给出错误。

Sub keyword_search_result()

    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim mydata As Object
    Dim myval As String

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = False
    IE.Navigate "http://www.platts.com/symbol-page-directories/symbol-search"

    Do
        DoEvents
    Loop Until IE.ReadyState = 3

    Do
        DoEvents
    Loop Until IE.ReadyState = 4

    IE.Document.getElementById("ctl00_ctl00_contentBody_contentMain_txtSearchSymbol").Value = "AABDG27"
    IE.Document.getElementById("ctl00_ctl00_contentBody_contentMain_btnSearch").Click

    Do
        DoEvents
    Loop Until IE.ReadyState = 4


    Set mydata = IE.Document.getElementsByClassName("divCellBottomMiddle")(0).getElementsByTagName("br")

     i = 0
     For Each objCollection In mydata
     myval = mydata(i).PreviousSibling.wholeText
     'Debug.Print i&; " "; myval
     i = i + 1
     If i = 11 Then
     'this is the case when myval string will come as "ESZ" 
     'but with lot of space
     Sheets("Sheet1").Range("A1") = Trim(Right(myval, 10))
     End If
     Next objCollection

     IE.Quit
     Set IE = Nothing


End Sub

那么应该改变什么,有没有更好的方法来使用 winhttprequest/XMLHTTP 或类似的东西?

【问题讨论】:

    标签: html vba internet-explorer


    【解决方案1】:

    另一个想法是检查 HTML-Element-Collection 是否不是 Nothing 以及它是否包含一些元素。否则,当使用“搜索”找不到任何内容时,会因为访问不在集合中的元素而发生错误。

    Dim cellBottomMiddle ' HTML-Element-Collection 
    Set cellBottomMiddle = IE.Document.getElementsByClassName("divCellBottomMiddle")
    
    If cellBottomMiddle Is Nothing Then GoTo finalize
    If cellBottomMiddle.Length <= 0 Then GoTo finalize
    
    Set mydata = cellBottomMiddle(0).getElementsByTagName("br")
    
    ' Some code here ...
    
    finalize:
        IE.Quit
        Set IE = Nothing
    

    【讨论】:

      【解决方案2】:

      我重新编写了您的代码,使其更易于理解,并更容易在表格中查找元素。

      我通过将元素添加到字符串数组来更改元素的查找方式。从这里您可以简单地选择数组中的最后一项,而不是遍历它来查找最后一项。我还将 IE Object 的等待部分添加到单独的 Sub 中以减少代码量。

      这是代码,我已经完成了。

      Sub keyword_search_result()
          Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
          Dim StrArray() As String
      
          With IE
              .Visible = False
              .Navigate "http://www.platts.com/symbol-page-directories/symbol-search"
              'Wait for the page to load
              IELoad IE
              .Document.getElementById("ctl00_ctl00_contentBody_contentMain_txtSearchSymbol").Value = "AABDG27"
              .Document.getElementById("ctl00_ctl00_contentBody_contentMain_btnSearch").Click
              'Wait again
              IELoad IE
          End With
      
          'Put the string into an array, split by line breaks
          StrArray = Split(IE.Document.getElementsByClassName("divCellBottomMiddle")(0).InnerText, vbCrLf)
      
          'Retrieve the last element in the array, place in Excel Range A1
          Sheets("Sheet1").Range("A1") = StrArray(UBound(StrArray))
      
          'Clean Up
          IE.Quit
          Set IE = Nothing
      End Sub
      
      Public Sub IELoad(Browser As Object)
      
          While Browser.ReadyState <> 4 Or Browser.Busy
              Application.Wait (Now() + TimeValue("00:00:01"))
          Wend
      
      End Sub
      

      【讨论】:

      • @Ryan.. 非常不幸的是,它在 StrArray = Split(IE.Document..
      • 您完全按照上面的方式运行代码?例如。没有做任何改变?你运行的是什么版本的IE?我相信 IE 8+ 支持 getElementsByClassName 。我的工作没有问题。如果此错误仍然出现,请尝试在导致问题的行之前添加另一个 IeLoad IE
      • 更新:我认为 IE 9+ 版本适用于 getElementsByClassName。见:w3schools.com/jsref/met_element_getelementsbyclassname.asp
      猜你喜欢
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 2015-03-09
      • 1970-01-01
      • 2016-10-07
      相关资源
      最近更新 更多