【发布时间】:2015-02-05 01:55:13
【问题描述】:
我正在编写 VBA 代码以从网站 (https://app.buzzsumo.com/top-content) 中提取数据。我有一个运行没有错误的功能代码,但是当点击命令运行时,我仍然无法让网页实际提交表单。我尝试了许多不同的方法和提交表单/单击提交按钮的组合,但到目前为止似乎都没有奏效。以下是我当前的代码。
Sub clickFormButton()
Dim ie As Object
Dim form As Variant,
Dim button As Variant
'add the “Microsoft Internet Controls” reference in VBA Project
Set ie = CreateObject("InternetExplorer.Application")
'using input box to enter URL I am serching for
Search_URL = InputBox("Enter URL to Search For")
With ie
.Visible = True
.navigate ("https://app.buzzsumo.com/#/top-content")
'Ensure that the web page downloads completely
While ie.ReadyState <> 4
DoEvents
Wend
'assigning the input variables to the html elements of the form
ie.document.getElementsByName("q").Item.innertext = Search_URL
'finding and clicking the button
Set objInputs = ie.document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Title Like "Press Enter to Search" Then
ele.Click
End If
End With
End Sub
我也尝试过其他方法来查找并点击按钮,例如:
'Dim i As Variant
'Set form = ie.document.getElementsByClassName("btn btn-highlight")
'For i = 1 To 5
'If form.Item(i).DefaultValue = "Search!" Then
'Set button = form.Item(i)
'button.Click
'End If
'Next i
请就我可能遗漏的内容或如何获取此代码以实际提交表单并进入搜索结果提供任何建议。提前感谢您提供的任何帮助!
这里有一些额外的细节:不幸的是,我试图点击的元素(“搜索”按钮)没有与之关联的 ID 或名称。这就是为什么要尝试替代方法的原因,例如遍历所有对象并尝试找到具有正确“标题”的对象。以下是 DOM 资源管理器中元素的代码:
<input title="Press Enter to search" class="btn btn-highlight" type="submit" ng-disabled="topContentSearchForm.$invalid" value="Search!"/>
与之相关的唯一属性是:
class: btn btn-highlight
type: submit
ng-disabled: topContentSearchForm.$invalid
value: Search!
title: Press Enter to Search
如果有其他方法可以找到元素 ID/名称,请告诉我?或者是否有另一种方法可以在没有这些属性的情况下单击按钮?谢谢
【问题讨论】:
-
您需要识别适当的 HTML 元素。 HTML 中的元素是什么,它的属性是什么?
-
谢谢大卫,我已将 HTML 元素信息添加到上面的帖子中,以及有关正在发生的事情的更详细说明。关于如何在没有元素 ID 或名称的情况下成功单击此搜索按钮,您还有什么建议吗?谢谢!
-
这可能需要 form.submit 但我现在没有时间看这个。我会在下午晚些时候尝试检查一下。
-
好的,谢谢大卫!我真的很欣赏你能提供的任何意见。
标签: html vba web-scraping