vba VBA访问远程网站,在已经自动点击其他网页上的按钮后自动点击提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8816557/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:53:56  来源:igfitidea点击:

VBA access remote website, automate clicking submit after already automating clicking button on other webpage

vbawebautomationclickremote-server

提问by brettdj

I'm working with vba in excel 2010 and internet explorer 8 and Vista. The code below works to go to a remote website and post a form. On the resulting page, the code should click the "get estimates" button. Instead I get this error "object variable or with block variable not set". The highlighted problem line in the code is "estimate = ieApp.document.getElementById("btnRequestEstimates")".

我正在 excel 2010 和 Internet Explorer 8 和 Vista 中使用 vba。下面的代码可用于访问远程网站并发布表单。在结果页面上,代码应单击“获取估计值”按钮。相反,我收到此错误“对象变量或未设置块变量”。代码中突出显示的问题行是“estimate = ieApp.document.getElementById("btnRequestEstimates")”。

I think part of the problem might be that the button that isn't working is a submit button that isn't part of a form. I am also wondering if the variables need to be reset before the 2nd button click. The error message implies this is a qualification problem, but I think it's a pretty standard way of qualifying an element in this situation. Those are some things I've been googling to no avail, I'm not really sure what the problem is.

我认为问题的一部分可能是不起作用的按钮是一个不属于表单的提交按钮。我还想知道是否需要在单击第二个按钮之前重置变量。错误消息暗示这是一个限定问题,但我认为这是在这种情况下限定元素的一种非常标准的方法。这些是我一直在谷歌搜索无济于事的一些事情,我不确定问题是什么。

Sub btn_version()

Dim ieApp As Object
Dim ieDoc As Object
Dim ieForm As Object
Dim ieObj As Object
Dim URL As String
Dim estimate As Object

URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.navigate URL
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend

Set ieDoc = ieApp.document
Set ieForm = ieDoc.forms(1)
For Each ieObj In ieForm.Elements
If ieObj.ClassName = "AddToCartButton" Then
ieObj.Click
End If
Next ieObj

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
estimate = ieApp.document.getElementById("btnRequestEstimates")
estimate.submit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub

回答by brettdj

The code below combines your xmlhttp code from automate submitting a post form that is on a website with vba and xmlhttp(to give you better control on the POST, ie skipping your Set ieDoc = ieApp.documentsection in our question) with clicking the "btnRequestEstimatesbutton on the final URl from this page

下面的代码结合了您的 xmlhttp 代码,从自动提交网站上的帖子表单与 vba 和 xmlhttp(为了让您更好地控制POST,即跳过Set ieDoc = ieApp.document我们问题中的部分),并单击此页面"btnRequestEstimates的最终 URl 上的按钮

Sub Scrape2()

Dim objIE As Object
Dim xmlhttp As Object
Dim ieButton As Object
Dim strResponse As String
Dim strUrl As String

strUrl = "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

'~~> Indicates that page that will receive the request and the type of request being submitted
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
'~~> Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'~~> Send the data as name/value pairs
xmlhttp.Send "Quantity=1&VariantID=2705&ProductID=2688"
strResponse = xmlhttp.responseText
objIE.navigate strUrl
objIE.Visible = True

Do While objIE.readystate <> 4
    DoEvents
Loop

objIE.document.Write strResponse

Set xmlhttp = Nothing
Set ieButton = objIE.document.getelementbyid("btnRequestEstimates")
ieButton.Click

End Sub