【问题标题】:Problems Using VBA to Submit a Web Page - Using the click button function but web page won't submit使用 VBA 提交网页的问题 - 使用点击按钮功能,但网页不会提交
【发布时间】: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


【解决方案1】:

我知道这是一篇旧帖子,但是……我一直在有效地使用它……

   '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

【讨论】:

    【解决方案2】:

    几个想法:

       While ie.ReadyState <> 4
           DoEvents
       Wend
    

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

    其次,我不明白为什么您需要遍历网页上的所有对象。更简单的方法是在 IE 中访问该网页,按 F12 并在 DOM 资源管理器中选择元素,您可以获得按钮的 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
    

    【讨论】:

    • 您好 Jeanno,感谢您的建议!就元素 ID 或名称而言,DOM 浏览器不会列出元素的这些属性。我在上面添加了其他详细信息,包括来自 DOM 资源管理器的代码。如果您有任何其他建议,请告诉我,再次感谢!
    • 请查看我的更新回复,如果您满意,请标记为答案。
    • 嗨 Jeanno,感谢更新的代码,我已经能够毫无错误地运行它,但是当代码完成时它仍然无法将我导航到搜索结果页面,当你运行?也许我错过了什么?我还手动运行了您为 i = 1 到 20 提供的循环代码,我也发生了同样的事情,它导航到项目编号 3 的定价页面,因此我们与该部分位于同一页面上。我只是不确定为什么当我使用您的完整代码时它没有导航到搜索结果页面。再次感谢,任何其他建议将不胜感激。
    • Application.Wait 通常并不理想。一个准备好的等待循环,比如Do While Not Ie.ReadyState=4 And Not IE.busy: Loop,通常更可取:)
    【解决方案3】:

    看起来您可能只构建字符串 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 窗口中进行了一些探索,这也应该可以工作,从您的代码修改。这就是我在评论 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
    

    【讨论】:

    • 谢谢你,大卫,这很好地解决了我的问题!!......而且我每周只搜索大约 20 个网站,我可以手动完成,但这要快得多。非常感谢!
    • 这也适用,从您的原始代码:ie.document.getElementsByClassName("btn btn-highlight").Item(4).form.submit
    【解决方案4】:

    CSS 选择器:

    您可以使用#search-btn &gt; div 的CSS 选择器。这是类名search-btn 中的div"#" 表示班级。


    VBA:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 1970-01-01
      • 2013-06-18
      • 2017-10-16
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多