I do not know how to send a request to a website, with a parameter set to a specific value, using MSXML2.xmlhttp.
Edit as suggested:
SSlinky in the comments states my trial is technically correct, that's why I want to show a more pertinent example.
I succeed to get the home page of the site.
myURL = "https://dictionary.cambridge.org/it/translate/"
xmlhttp.Open "GET", myURL, False
xmlhttp.send
html.body.innerHTML = xmlhttp.responseText
Set form = html.getElementsByClassName("clrd nocontain")(0)
action = "https://dictionary.cambridge.org/it/translate/"
Then collect all hidden inputs
Set inputList = form.getElementsByTagName("input")
For Each hiddenInput In inputList
If InStr(hiddenInput.outerHTML, "hidden") Then
If querystring = "" Then
querystring = hiddenInput.getAttribute("name") & "=" & hiddenInput.getAttribute("value")
Else
querystring = querystring & "&" & hiddenInput.getAttribute("name") & "=" & hiddenInput.getAttribute("value")
End If
End If
Next
Then I add the specific input value, and call the POST
body = "txtTrans=dog&" & querystring
Call xmlhttp.Open("POST", action, False)
Call xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Call xmlhttp.send(body)
If xmlhttp.Status <> 200 Then Exit Sub
html.body.innerHTML = xmlhttp.responseText
I would expect I find the following result into the responseText
<div id="translate-tool__result" class="tc-bb translate-tool__result translate-english-spanish js-translator-result" lang="it">
cane
</div>
Instead I find
<div class="tc-bb translate-tool__result translate-english-spanish js-translator-result" id="translate-tool__result">
</div>
with no result.
Previous Question:
I simulated my problem with the example below, where I assign the value "dog" to the input "si". The action is taken from the form.
<input type="text" name="w" id="si" oninvalid="alert('Caratteri non validi nella casella di ricerca');" pattern="^[^<>%\\]*$" size="20" maxlength="200" tabindex="1" onkeydown="keyDown(event)" accesskey="s" title="accesskey: s" value="" placeholder="" autocomplete="off" autocorrect="off" autocapitalize="none">
Dim xmlhttp As New MSXML2.xmlhttp
Dim html As New HTMLDocument
Dim action As String
Dim body As Variant
action = "https://www.wordreference.com/redirect/translation.aspx"
body = "si=dog"
Call xmlhttp.Open("POST", action, False)
Call xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Call xmlhttp.send(body)
If xmlhttp.Status <> 200 Then Exit Sub
html.body.innerHTML = xmlhttp.responseText
Debug.Print html.getElementsByClassName("ToWrd").Length
Into the response I do not get the <TD> below (.length is always 0), that is meant to contain the result of the request, that I get if I interact by the web.
<td class="ToWrd">cane <em class="POS2 tooltip" data-lang="it" data-loaded="true">nm<span><i>sostantivo maschile</i>: Identifica un essere, un oggetto o un concetto che assume genere maschile: <b>medico, gatto, strumento, assegno, dolore</b> </span></em><i class="copy"></i></td>
What am I missing here?