I was wondering if it is possible to "click a button" on a webpage without opening the page in IE. The webpage is dynamically generated and the click on the button calls a script that changes the content of the page.
I am able to do this opening Internet Explorer with this sub:
Sub DownloadPageScript(strUrl As String, htmlPage As htmlDocument, strScript As String)
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
IE.navigate strUrl
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
' Run the scripts associated to the button to get the data
IE.Document.parentWindow.execScript strScript, "jscript"
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Set htmlPage = IE.Document
End Sub
But I would like to avoid opening Internet Explorer so I would like to something like this:
Sub Download_Page(strUrl As String, htmlPage As htmlDocument, strScript As String)
Dim xmlHttp As Object
'
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
xmlHttp.Open "GET", strUrl, False
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'
' Here I should add something to execute the script
'
' After execution
'
Set htmlPage = New htmlDocument
htmlPage.body.innerHTML = xmlHttp.ResponseText
'
End Sub
I was expecting to find something like a xmlHttp.execute(args)
method to replicate the action of clicking the button but I was wrong.
So my question is: Is it possible to replicate the button click if I do not want to open Internet Explorer? and if yes what should I do?
New approach based on the idea in the comments
I tried the approach suggested by @omegastripes in the comments and I wrote this sub taken by his answer 33484763:
Sub TestDownload()
Dim xmlHttp As Object
Dim htmlPage As htmlDocument
Dim strExportURL As String
Dim strFormData As Variant
Dim strContent As String
' build exportURL parameter
strExportURL = Join(Array( _
"p_p_id=ScommesseAntepostPalinsesto_WAR_scommesseportle", _
"p_p_lifecycle=2", _
"p_p_resource_id=dettagliManifestazione", _
"p_p_cacheability=cacheLevelPage", _
"_ScommesseAntepostPalinsesto_WAR_scommesseportlet_codDisc=1", _
"_ScommesseAntepostPalinsesto_WAR_scommesseportlet_codMan=21", _
"_ScommesseAntepostPalinsesto_WAR_scommesseportlet_codScomm=3", _
"_ScommesseAntepostPalinsesto_WAR_scommesseportlet_codClusterScomm=80", _
"_ScommesseAntepostPalinsesto_WAR_scommesseportlet_filtro=0" _
), "&")
' build the whole form data
strFormData = Join(Array( _
"languageCode=en", _
"exportURL=" & URLEncode(strExportURL) _
), "&")
' POST XHR to retrieve the content
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
xmlHttp.Open "POST", "http://www.sisal.it/scommesse-matchpoint/palinsesto", False
xmlHttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.send strFormData
Set htmlPage = New htmlDocument
htmlPage.body.innerHTML = xmlHttp.responseText
'
End Sub
Public Function URLEncode( _
StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
Dim bytes() As Byte, b As Byte, i As Integer, space As String
If SpaceAsPlus Then space = "+" Else space = "%20"
If Len(StringVal) > 0 Then
With New ADODB.Stream
.Mode = adModeReadWrite
.Type = adTypeText
.Charset = "UTF-8"
.Open
.WriteText StringVal
.Position = 0
.Type = adTypeBinary
.Position = 3 ' skip BOM
bytes = .Read
End With
ReDim result(UBound(bytes)) As String
For i = UBound(bytes) To 0 Step -1
b = bytes(i)
Select Case b
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
result(i) = Chr(b)
Case 32
result(i) = space
Case 0 To 15
result(i) = "%0" & Hex(b)
Case Else
result(i) = "%" & Hex(b)
End Select
Next i
URLEncode = Join(result, "")
End If
End Function
The URLEncode()
function is from this post URLEncode. (I tried to use JScriptControl but It does not work probably because I have Office 64-bit).
This code runs without errors but when I look to the content of htmlPage
it is almost empty. I think the problem is that the request I am sending is wrong but I am not able to correct it, can you help me?