1

Ive been going through many similar questions, like this and this but mine is much simpler.I want to change the date on a webform and get the data using POST request

I have this code which makes a POST request:

Sub winpost()
Dim WebClient As WinHttp.WinHttpRequest
Set WebClient = New WinHttp.WinHttpRequest
Dim searchResult As HTMLTextElement: Dim searchTxt As String
Dim html As New HTMLDocument

Dim Payload As String
Payload = "ContentPlaceHolder1_ddlday=6"

With WebClient
.Open "POST", "http://pib.nic.in/AllRelease.aspx", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send (Payload)
.waitForResponse
End With

html.body.innerHTML = WebClient.responseText
Set searchResult = html.querySelector(".search_box_result"): searchTxt = searchResult.innerText
Debug.Print searchTxt

End Sub

The website is this.The page sends a post request onchange of any fields. On looking at ChromeDevTools under network > Formdata section i see this:

ctl00$ContentPlaceHolder1$ddlday: 8

I have tried various versions of this in the Payload string.But it always returns the same page (8th jan).

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
dan brown
  • 323
  • 5
  • 14
  • 3
    Seems like you're crawling asp.net website which uses viewstates. In otherwords it's stateful website so you can't just send POST request and get results. You need to replicate the state progression. I have no idea how to do that in VBA but it's not easy, you might want to take a look in web browser emulation tools such as Selenium. – Granitosaurus Jan 08 '19 at 09:55
  • Check your post data: When I go to your web site and change the date, the post data is much more than just the day. It looks rather complex, doubt you will get the same result when posting the day only... – EarlyBird2 Jan 08 '19 at 09:59
  • Yeah there are a lot of other formdata fields.I guess I will try using the ie object from VBA.(only have access to excel here so no selenium,node etc).Thanks for pointing the viewstates thing out. – dan brown Jan 08 '19 at 10:03

1 Answers1

1

Internet Explorer

With IE slightly different syntax from selenium basic (shown at bottom) as no SelectByText option. You can use indices or attribute = value css selectors for example. Here months are indices upto 12 instead of month names

Option Explicit    
Public Sub SetDates()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "http://pib.nic.in/AllRelease.aspx"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .Document
            .querySelector("#btnSave").Click
            .querySelector("#ContentPlaceHolder1_ddlMonth [value='2']").Selected = True
            .querySelector("#ContentPlaceHolder1_ddlYear [value='2018']").Selected = True
            .querySelector("#ContentPlaceHolder1_ddlday [value='2']").Selected = True
        End With
        Stop '<==delete me later
        .Quit
    End With
End Sub

Selenium basic:

If you do go down the selenium basic vba route you can do something like as follows. Note: You would need to go VBE > Tools > References > Add reference to selenium type library after installing selenium. You would also need latest Chrome and ChromeDriver and ChromeDriver folder should be placed on environmental path or chromedriver placed in folder containing selenium executables.

Option Explicit
Public Sub SetDates()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const Url = "http://pib.nic.in/AllRelease.aspx"
    With d
        .Start "Chrome"
        .get Url
        .FindElementById("btnSave").Click

        'date values
        .FindElementById("ContentPlaceHolder1_ddlMonth").AsSelect.SelectByText "February"
        .FindElementById("ContentPlaceHolder1_ddlYear").AsSelect.SelectByText "2018"
        .FindElementById("ContentPlaceHolder1_ddlday").AsSelect.SelectByText "2"
        Stop   'delete me later

        .Quit
    End With
End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Wow!Very clean solutions,did not know about selenium basic,Tested them and they seem to work.Thanks a bunch – dan brown Jan 08 '19 at 11:03