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

后端 未结 1 444
情歌与酒
情歌与酒 2020-12-22 05:02

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 shou

相关标签:
1条回答
  • 2020-12-22 05:15

    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
    
    0 讨论(0)
提交回复
热议问题