VBA访问远程网站,自动化已经自动在其他网页上点击按钮后,点击提交 [英] VBA access remote website, automate clicking submit after already automating clicking button on other webpage

查看:504
本文介绍了VBA访问远程网站,自动化已经自动在其他网页上点击按钮后,点击提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2010年Excel VBA中和Internet Explorer 8和Vista工作。下面的code工作去一个远程网站,并张贴的形式。在结果页面中,code应该点击获取估计按钮。相反,我得到这个错误对象变量或没有设置块变量。突出显示的问题线在code是估计= ieApp.document.getElementById(btnRequestEstimates)。

我认为问题的一部分,可能是不工作的按钮是一个提交按钮,是不是一种形式的一部分。我也想知道如果变量需要第二个按钮点击之前复位。该错误消息意味着这是一个资格的问题,但我认为这是在这种情况下预选赛元素的pretty标准方式。这些都是一些事情我一直在google搜索无果,我真的不知道是什么问题。

 子btn_version()昏暗ieApp作为对象
昏暗ieDoc作为对象
昏暗ieForm作为对象
昏暗ieObj作为对象
昏暗的URL作为字符串
昏暗的估计作为对象URL =htt​​p://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx
设置ieApp =的CreateObject(InternetExplorer.Application)
ieApp.Visible = TRUE
ieApp.navigate网址
虽然ieApp.Busy或者ieApp.readyState<> 4:调用DoEvents:WEND设置ieDoc = ieApp.document
设置ieForm = ieDoc.forms(1)
对于每个ieObj在ieForm.Elements
如果ieObj.ClassName =AddToCartButton然后
ieObj.Click
万一
接下来ieObj'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
虽然ieApp.Busy或者ieApp.readyState<> 4:调用DoEvents:WEND
估计= ieApp.document.getElementById(btnRequestEstimates)
estimate.submit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''结束小组


解决方案

在code从下面的自动提交帖子形式,是使用VBA和XMLHTTP 在网站上(给​​你的 POST 更好的控制,也就是跳过你的点击与从<一个对最终URL中的btnRequestEstimates 按钮,在我们的问题设置ieDoc = ieApp.document 章节) href=\"https://www.craft-e-corner.com/ShoppingCart.aspx?add=true&ReturnUrl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge\"相对=nofollow>此页面

 子Scrape2()昏暗objIE作为对象
昏暗的XMLHTTP作为对象
昏暗ieButton作为对象
昏暗strResponse作为字符串
昏暗strUrl作为字符串strUrl = \"http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge\"
设置objIE =的CreateObject(InternetExplorer.Application)
objIE.navigate关于:空白
设置XMLHTTP =的CreateObject(MSXML2.ServerXMLHTTP)~~&GT;表示所提交该网页将接收请求和请求类型
xmlhttp.OpenPOST, \"http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge\",假
~~&GT;表明请求的主体包含表单数据
xmlhttp.setRequestHeader内容类型,应用程序/ x-WWW的形式urlen codeD
~~&GT;发送该数据作为名称/值对
xmlhttp.Send数量= 1&安培; VariantID = 2705&放大器;的ProductID = 2688
strResponse = xmlhttp.responseText
objIE.navigate strUrl
objIE.Visible = TRUE做虽然objIE.readystate&LT;&GT; 4
    的DoEvents
循环objIE.document.Write strResponse设置XMLHTTP =什么
设置ieButton = objIE.document.getelementbyid(btnRequestEstimates)
ieButton.Click结束小组

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")".

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

解决方案

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.document section in our question) with clicking the "btnRequestEstimates button on the final URl from this page

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

这篇关于VBA访问远程网站,自动化已经自动在其他网页上点击按钮后,点击提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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