【发布时间】:2019-04-06 02:56:15
【问题描述】:
我正在使用 IE11 并激活了 HMTL 对象库和 Internet 控件引用。
按钮上没有元素 ID,但可以通过添加一些 html 和 xml 声明来使用ie.Document.getElementsByClassName,这要感谢this post。
我从 Excel 中获取名称和城市、州并将其插入网站,然后单击搜索按钮。
这是我的错误发生的地方。
运行时错误“438”:对象不支持此属性或方法。
VBA:
Option Explicit
Sub HGScrape()
'Application.ScreenUpdating = False
'we define the essential variables
Dim ie As Object
Dim my_url As String
Dim SearchButton, NameBox, AddressBox
Dim ele As Object
Dim x As Integer
Dim y As Integer
Dim IsOdd As Integer
Dim html_doc As Object 'HTMLDocument
Dim xml_obj As Object 'MSXML2.DOMDocument
my_url = "https://www.healthgrades.com/"
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = New InternetExplorer
ie.Visible = True 'False ''''''''''''''''''''''
ie.Navigate my_url '"13.33.74.92" '("https://www.healthgrades.com/")
While ie.ReadyState <> 4
DoEvents
Wend
Set NameBox = ie.Document.getElementById("search-term-selector-child")
NameBox.Value = ActiveSheet.Range("A2")
Set AddressBox = ie.Document.getElementById("search-location-selector-child")
AddressBox.Value = ActiveSheet.Range("B2")
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerHTML = xml_obj.responseText
Set SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter") 'id of the button control (HTML Control)
SearchButton.Click
While ie.ReadyState <> 4
DoEvents
Wend
【问题讨论】:
-
你能试试
ie.document.forms(0).Submit吗? -
您说您引用了 MS Internet 控件和 MS HTML Obj 库 - 但您仍然将变量声明为
As Object类型? -
好像刚刚刷新了页面
-
好的,我去看看
-
这篇文章使用
excel-vba标签,即pending removal和should be replaced byexcel和vba。但由于它已经达到了 5 个标签的限制,如果不删除至少一个其他标签,就不可能建议编辑...
标签: html excel vba web-scraping getelementsbyclassname