使用VBA提交网页的问题 - 使用单击按钮功能但网页无法提交

时间:2015-02-05 01:55:14

标签: html vba web-scraping

我正在编写VBA代码以从网站(https://app.buzzsumo.com/top-content)提取数据。我有一个没有错误运行的功能代码,但是当click命令运行时,我仍然无法让网页实际提交表单。我已经尝试了许多不同的方法和提交表单/单击提交按钮的组合,但到目前为止似乎没有任何工作。以下是我目前的代码。

 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

请提供有关我可能遗失的内容或如何获取此代码以实际提交表单并前往搜索结果的任何建议。提前感谢您提供的任何帮助!

以下是一些其他细节:不幸的是,我尝试点击的元素(&#34;搜索&#34;按钮)没有与之关联的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 /名称,请告诉我们?或者如果有其他方法点击没有这些属性的按钮?感谢

4 个答案:

答案 0 :(得分:3)

我知道这是一个很老的帖子,但是......我一直在有效地使用它。

   'click login
            Set htmlDoc = .document
            Set htmlColl = htmlDoc.getElementsByTagName("input")
            Do While htmlDoc.readyState <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If Trim(htmlInput.Type) = "submit" Then
                        htmlInput.Click
                        Exit For
                    End If
                Next htmlInput

答案 1 :(得分:2)

一些想法:

   While ie.ReadyState <> 4
       DoEvents
   Wend

如果页面上有javascripts,请使用Application.Wait Now + TimeSerial(0, 0, 4)(基本上等待4秒)。

其次我不明白为什么你需要遍历网页上的所有对象。更简单的方法是在IE中访问该网页,点击F12并在DOM资源管理器中选择元素,您可以获取按钮的ID或名称,然后使用ie.document.GetElementByID("buttonID").Clickie.document.GetElementsByName("buttonName").Item.Click

如果有帮助,请告诉我。

编辑:在检查特定网页后,似乎缺少该按钮的ID和名称属性。所以我不得不采取以下措施:

Dim i As integer
Set form = ie.document.getElementsByClassName("btn btn-highlight")
On Error Resume Next
For i = 1 To 20
If form.Item(i).DefaultValue = "Search!" Then
     form.Item(i).Click
End If
Next i

第四个项目点击相关按钮(我必须手动完成循环,因为第三个项目从页面导航到定价页面,所以我不得不回去)。无论如何,完整的代码如下,请注意,如果网页发生变化,您将需要再次进行此练习

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")
    End With
    'wait for page to load
    Application.Wait Now + TimeSerial(0, 0, 5)
    'assigning the input variables to the html elements of the form
    ie.document.getElementsByName("q").Item.InnerText = Search_URL
    'finding and clicking the button
    ie.document.getElementsByClassName("btn btn-highlight").Item(4).Click
End Sub

答案 2 :(得分:1)

看起来您可能只是构建字符串URL,例如,如果您放置&#34; abcd&#34;在搜索字段中,生成的URL将为:

https://app.buzzsumo.com/top-content?result_type=total&type=articles&num_days=360&tfc=false&general_article&infographic&video&page=1&guest_post&giveaway&interview&links_sitewide=true&unique_domains=true&backlinks=false&q= ABCD &安培;偏移量= 0

注意作为搜索查询的粗体部分。

所以,这只是一个快速的想法,只要您不通过发送1000个自动请求来滥用他们的系统,这可能会有效:

Sub FetchWebsite()
    Dim ie As Object
    Dim form As Variant
    Dim button As Variant
    Dim url As String

    '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")

    '### BUILD THE FULL URL
    url = "https://app.buzzsumo.com/top-content?result_type=total&type=articles&num_days=360&tfc=false&general_article&infographic&video&page=1&guest_post&giveaway&interview&links_sitewide=true&unique_domains=true&backlinks=false&q=" & Search_URL & "&offset=0"

    With ie
        .Visible = True
        .navigate url
    End With
    'wait for page to load
    Do
    Loop While Not ie.ReadyState = 4 And Not ie.Busy

    AppActivate "Internet Explorer"

End Sub

我在Locals窗口中做了一些调试,这也应该可以使用,从代码中修改。这将是我在评论OP时提到的Form.Submit

Sub clickFormButton()
    Dim ie As InternetExplorer
    Dim form As Variant
    Dim button As Variant

    Dim ele As HTMLFormElement
    '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")
    End With
    'wait for page to load
    Do
    Loop While Not ie.ReadyState = 4 And Not ie.Busy

    'assigning the input variables to the html elements of the form
    ie.document.getElementsByName("q").Item.InnerText = Search_URL
    'finding and clicking the button

    ie.document.getElementsByClassName("btn btn-highlight").Item(4).form.submit
End Sub

答案 3 :(得分:1)

CSS选择器:

您可以使用#search-btn > div的CSS选择器。它是className div中的search-btn"#"表示班级。


VBA:

使用.querySelector方法来应用CSS选择器:

ie.document.querySelector("#search-btn > div").Click