使用 VBA 提交网页的问题 - 使用点击按钮功能但网页不会提交

Problems Using VBA to Submit a Web Page - Using the click button function but web page won't submit

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

 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

请就我可能遗漏的内容或我如何获得此代码以实际提交表单并前进到搜索结果提供任何建议。提前感谢您提供的任何帮助!

这里有一些额外的细节:不幸的是,我试图点击的元素("Search" 按钮)没有与之关联的 ID 或名称。这就是为什么它正在尝试替代方法,例如遍历所有 object 并尝试找到具有正确“标题”的那个。这是来自 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/name,请告诉我?或者是否有另一种方法可以在没有这些属性的情况下单击按钮?谢谢

几个想法:

   While ie.ReadyState <> 4
       DoEvents
   Wend

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

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

如果有帮助请告诉我。

编辑: 检查特定网页后,该按钮的 ID 和 Name 属性似乎丢失了。所以我不得不求助于以下方法:

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

看起来您可能只构建字符串 URL,例如,如果您在搜索字段中输入 "abcd",结果 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&offset=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 window 中进行了一些探索,这应该也可以,根据您的代码进行了修改。这就是我在评论 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

我知道这是旧的 post 但是...我一直有效地使用它..

   '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

CSS 选择器:

您可以使用 #search-btn > div 的 CSS 选择器。这是 class 名称 search-btn 中的 div"#" 表示 class.


VBA:

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

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