使用VBA提交网页时出现问题-使用单击按钮功能,但网页无法提交 [英] Problems Using VBA to Submit a Web Page - Using the click button function but web page won't submit

查看:138
本文介绍了使用VBA提交网页时出现问题-使用单击按钮功能,但网页无法提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写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/名称?还是如果有其他方法可以单击没有这些属性的按钮?谢谢

解决方案

您似乎可以仅构建字符串URL,例如,如果在搜索字段中输入"abcd",则得到的URL将是:

我在本地"窗口中进行了一些查找,并且也可以使用您的代码进行修改.这就是我在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

I am writing a VBA code to pull data from a website (https://app.buzzsumo.com/top-content). I have a functional code that runs without errors however I still can't get the webpage to actually submit the form when the click command runs. I have tried many different approaches and combinations of submitting the form/clicking the submit button but none have seemed to work so far. Below is my current code.

 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

I have also tried other methods to find and click the button such as:

'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

Please provide any recomendations on what I may be missing or how I can get this code to actually submit the form and advance to the search results. Thanks in advance for any help you can provide!

Here are some additional details: Unfortunately the element I am trying to click (the "Search" button) does not have an ID or Name associated with it. This is why is was trying alternative approaches, such as looping through all of the object and trying to find the one with the right "Title". Here is the code for the element from the DOM explorer:

<input title="Press Enter to search" class="btn btn-highlight" type="submit" ng-disabled="topContentSearchForm.$invalid" value="Search!"/>

The only attributes associated with it are:

class: btn btn-highlight
type: submit
ng-disabled: topContentSearchForm.$invalid
value: Search!
title: Press Enter to Search

Please let me know if there is another way to find the element ID/name? or if there is another way to click the button without these attributes? Thanks

解决方案

It looks like you could potentially just build the string URL, for example if you put "abcd" in the search field, the resulting URL will be:

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

Note the bolded portion which is the search query.

So, and this is just a quick idea that may work as long as you're not trying to abuse their system by sending 1000's of automated requests:

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

I did some poking around in the Locals window and this should also work, modified from your code. This would be the Form.Submit that I mentioned in comment on OP.

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

这篇关于使用VBA提交网页时出现问题-使用单击按钮功能,但网页无法提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆