vba 使用VBA提交网页的问题 - 使用点击按钮功能但网页不会提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28334911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems Using VBA to Submit a Web Page - Using the click button function but web page won't submit
提问by Michael Droesch
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.
我正在编写 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
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:
以下是一些其他详细信息: 不幸的是,我尝试单击的元素(“搜索”按钮)没有关联的 ID 或名称。这就是为什么要尝试其他方法,例如遍历所有对象并尝试找到具有正确“标题”的对象。以下是 DOM 资源管理器中元素的代码:
<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
请让我知道是否有其他方法可以找到元素 ID/名称?或者是否有另一种方法可以在没有这些属性的情况下单击按钮?谢谢
回答by Kdunc2015
I know this is an old post but... I have been using this effectively..
我知道这是一篇旧帖子,但是......我一直在有效地使用它......
'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
回答by Jeanno
A couple of ideas:
几个想法:
While ie.ReadyState <> 4
DoEvents
Wend
If you have javascripts on the page use Application.Wait Now + TimeSerial(0, 0, 4)
(basically wait for 4 seconds) instead.
如果页面上有 javascripts,请改用Application.Wait Now + TimeSerial(0, 0, 4)
(基本上等待 4 秒)。
Second I don't understand why you need to loop through all the objects on the web page. The easier way would be to go that webpage in IE, hit F12 and select element in DOM explorer, you can get the ID or Name of the button and then use ie.document.GetElementByID("buttonID").Click
or ie.document.GetElementsByName("buttonName").Item.Click
其次,我不明白为什么您需要遍历网页上的所有对象。更简单的方法是在 IE 中访问该网页,按 F12 并在 DOM 资源管理器中选择元素,您可以获得按钮的 ID 或名称,然后使用ie.document.GetElementByID("buttonID").Click
或ie.document.GetElementsByName("buttonName").Item.Click
Let me know if this helps.
如果这有帮助,请告诉我。
Edit:After inspecting the particular webpage it appears that the ID and Name attributes for that button are missing. So I had to resort to the following:
编辑:检查特定网页后,似乎缺少该按钮的 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
The relevant button is clicked for the fourth item (I had to manually go through the loop because 3rd item navigated away from the page to a pricing page, so i had to go back). Anyway the full code is the following, please note that you will need to go through this exercise again if there were changes to the webpage
单击第四项的相关按钮(我必须手动完成循环,因为第三项从页面导航到定价页面,所以我不得不返回)。无论如何,完整的代码如下,请注意,如果网页有更改,您将需要再次进行此练习
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
回答by David Zemens
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:
看起来您可能只构建字符串 URL,例如,如果您在搜索字段中输入“abcd”,则生成的 URL 将是:
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:
所以,这只是一个快速的想法,只要您不试图通过发送 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
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.
我在 Locals 窗口中进行了一些检查,这也应该可以工作,从您的代码修改。这就是Form.Submit
我在对 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
回答by QHarr
CSS selector:
CSS 选择器:
You can use CSS selector of #search-btn > div
. Which is div
within className search-btn
. "#"
means class.
您可以使用 .css 的 CSS 选择器#search-btn > div
。这是div
在 className 内search-btn
。"#"
意味着阶级。
VBA:
VBA:
Use .querySelector
method to apply CSS selector:
使用.querySelector
方法来应用 CSS 选择器:
ie.document.querySelector("#search-btn > div").Click