我在 VBA 中使用 xml。下面的代码将一个项目添加到远程网站上的购物车(发布表单)。然后代码在 Internet Explorer 中显示结果。
您可以在响应中看到“获取估计”按钮。我需要自动单击它,输入位置信息,并在我的 excel 2010 工作表上获取运费和税费(针对当前购物车中的商品)的响应。
我希望网站(网站的服务器?)直接进行所有自动点击和输入数据,就像我将商品添加到购物车时一样,如果可能的话,不要通过浏览器。页面加载需要很长时间,我认为如果我通过浏览器,无论如何我都可以在没有 xml 的情况下做到这一点。但我真的被卡住了,所以如果我必须加载一个浏览器也没关系。
Option Explicit
Sub testing()
Dim objIE As Object
Dim xmlhttp As Object
Dim response As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
objIE.Visible = True
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"
response = xmlhttp.responseText
objIE.Document.Write response
Set xmlhttp = Nothing
End Sub