VBA Excel - Accesso sito web, autenticarsi e navigare

Anonimo
2018-06-25T13:11:11+00:00

Ciao a tutti,

documentandomi tra vari form ho scritto il codice sotto per accedere ad un sito con username e password.

Non riesco a farlo funzionare, non mi si genera errore particolare ma le credenziali non vengono inserite.

Vi incollo sotto il codice e l'html.

Grazie in anticpo a chi verrà in mio soccorso :)

Felice

Sub prova_finale2()

Dim HTMLDoc As HTMLDocument

Dim oBrowser As InternetExplorer

Dim oHTML_Element As IHTMLElement

Dim sURL As String

On Error GoTo Err_Clear

sURL = "http://mhs354"

Set oBrowser = New InternetExplorer

oBrowser.Silent = True

oBrowser.timeout = 60

oBrowser.Navigate sURL

oBrowser.Visible = True

Do

' Wait till the Browser is loaded

Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.UserId.Value = "user id"

HTMLDoc.all.UserPassword.Value = "password"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")

If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For

Next

' oBrowser.Refresh ' Refresh If Needed

Err_Clear:

If Err <> 0 Then

Err.Clear

Resume Next

End If

End Sub

--------------------------------html

<tr valign="top">

<td colspan="3">

<div class="systemInfoDiv" id="systemInfoDiv">

<font class="systemInfoText" id="systemInfoText" name="systemInfoText"></font>

<div style="display: none;">

<embed name="warningSound" src="warningSignal.wav" type="audio/x-wav" hidden="true" loop="false" autostart="false">

</div>

</div>

<div class="mainDiv" id="mainDiv" style="height: auto;">

<!-- commentStart bodyContent -->

<form name="bsUserForm" onsubmit="return false;">

<script>

                VoNavigationArray["OutputScreen"] = "W0001";

            </script><input name="VoLoginUserIn" class="startTag" type="hidden" value="">

<table align="center" class="formTable" style="width: 190px; margin-top: 10px;">

<tbody><tr>

<td colspan="2">

<div align="center" style="font-size: 12pt; background-color: rgb(173, 216, 230);">

<font class="labelText"><b>Login</b></font>

</div>

</td>

</tr>

<tr>

<td style="border: 1px dashed rgb(204, 204, 204);" colspan="2">

<div style="margin-top: 8px; float: left;">

<font class="labelText">User Name: </font>

</div>

<div style="float: right;">

<input name="UserId" tabindex="2" class="tag" id="UserId" style="border: 2px solid rgb(103, 131, 158); color: black; background-color: rgb(255, 255, 255);" onclick="this.select();" onblur="this.value = this.value.toUpperCase();" type="text" size="12" maxlength="15" value="">

</div>

</td>

</tr>

<tr>

<td style="border: 1px dashed rgb(204, 204, 204);" colspan="2">

<div style="margin-top: 8px; float: left;">

<font class="labelText">Password: </font>

</div>

<div style="float: right;">

<input name="UserPassword" tabindex="3" class="tag" id="UserPassword" style="-webkit-text-security: disc;" onclick="this.select();" type="password" size="12" maxlength="15" pattern="[0-9]*" value="">

</div>

</td>

</tr>

<tr>

<td style="border: 1px dashed rgb(204, 204, 204);" colspan="2">

<div style="margin-top: 8px; float: left;">

<font class="labelText">Lingua:</font>

</div>

<div style="float: right;">

<select name="LangCode" tabindex="4" class="tag" id="LangCode"><option value="--">--</option><option value="IT">IT</option><option value="AU">AU</option><option value="BF">BF</option><option value="BL">BL</option><option value="CE">CE</option><option value="CF">CF</option><option value="CN">CN</option><option value="CZ">CZ</option><option value="DE">DE</option><option value="DK">DK</option><option value="ES">ES</option><option value="FI">FI</option><option value="FR">FR</option><option value="GB">GB</option><option value="HR">HR</option><option value="HU">HU</option><option value="IN">IN</option><option value="JP">JP</option><option value="KR">KR</option><option value="NL">NL</option><option value="NO">NO</option><option value="PL">PL</option><option value="PT">PT</option><option value="RO">RO</option><option value="RS">RS</option><option value="RU">RU</option><option value="SE">SE</option><option value="SK">SK</option><option value="SL">SL</option><option value="US">US</option></select>

</div>

</td>

</tr>

<tr class="zoomToggle" style="display: none;">

<td style="border: 1px dashed rgb(204, 204, 204);" colspan="2">

<div style="margin-top: 8px; float: left;">Zoom:</div>

<div style="float: right;">

<select name="zoom" onchange="setCookieVal('zoom',value);"><option value="off">OFF</option><option value="on">ON</option></select>

</div>

</td>

</tr>

<tr>

<td align="right" colspan="2"><input name="ScreenWidth" class="tag" id="ScreenWidth" type="hidden" value=""><input name="ScreenHeight" class="tag" id="ScreenHeight" type="hidden" value=""><input name="Application" class="tag" id="Application" type="hidden" value="UNI"><input name="VoLoginUserIn" class="endTag" type="hidden" value="">

<div style="margin-top: 1px; float: right;">

<input name="loginButton" tabindex="100" class="btn" onkeydown="return false;" onclick="submitBsForm();" type="button" value="Login">

</div>

</td>

</tr>

<tr>

<td colspan="2">

<div align="center" style="font-size: 12pt; background-color: rgb(173, 216, 230);">MHS354  <br>

</div>

</td>

</tr>

</tbody></table>

<div align="center">

<div align="center" class="indexImgFrame" id="infoservicesDiv">

<a id="ofcS1" onclick="srvGetURL(this);" href="service?App=S1&Auto=0"><img class="borderZero" src="infoDisplayLogo.png?v=1804031518"></a>

</div>

<br>

</div>

<br>

</form>

<form name="bsForm" id="bsForm" action="fcgi_default" method="post">

<input name="bsXML" id="bsXML" type="hidden" value="">

</form>

<script>

            bsHeaderArray["BS"] = "BsLoginUser";

            bsHeaderArray["EBC"] = "EBCJSGFADM01";

            //bsHeaderArray["XMLSize"] = "300000";

            //bsHeaderArray["Timeout"] = "2000";

            bsHeaderArray["XSLTtransform"] = "SERVER";

            //commonInputArray["InputScreen"] = "WL001";

            //commonInputArray["User_Id"] = "HELP";

        </script><script>

        /*ULND081114 start*/

        validateForm.functions = function(){

        /*LIYY130109*/

        /* ULND090114 - Get window dimensions (instead of screen resolution) */

            document.getElementById("ScreenWidth").value = getWindowWidth(); /*ULND090303*/

            document.getElementById("ScreenHeight").value = getWindowHeight(); /*ULND090303*/

            if(!validateForm.notEmpty(document.forms["bsUserForm"]["UserId"])){

                document.getElementById('systemInfoText').innerHTML = "User Name Invalido";

                return false;

            }

            /* JUOETT131222_IKEA00307818  shpar8_1053729 */

            if(!validateForm.stringLength(document.forms["bsUserForm"]["UserPassword"], 4, 15)){

                document.getElementById('systemInfoText').innerHTML = "Password non valida";

                return false;

            }

            return true;

        }

        /*ULND081114 end*/

        </script>

<!-- commentStop bodyContent -->

</div>

</td>

</tr>

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

3 risposte

Ordina per: Più utili
  1. Anonimo
    2018-06-30T05:44:31+00:00

    Difficile da dire senza avere la pagina web da analizzare nei singoli elementi.

    Ti posso dire che provando su un altro sito (in particolare INPS

    https://serviziweb2.inps.it/PassiWeb/jsp/login.jsp?uri=https%3a%2f%2fwww.inps.it%2fnuovoportaleinps%2fdefault.aspx%3faccessoinps%3d1&S=S

    le credenziali vengono inserite ma poi non trovando alcun elemento con il NOME TAG  "input" non esegue l'accesso.

    Ma nel caso del sito INPS si può utilizzare il riferimento al form per eseguire il submit.

    Rispetto al tuo codice io ho solo modificato i riferimenti ai campi utente e password e aggiunto il comando del submit con riferimento diretto all'id del "form"

    HTMLDoc.all.utente.Value = CODICEFISCALE

    HTMLDoc.all.PASSWORD.Value = PIN

    HTMLDoc.all.Page.submit

    e la procedura esegue l'accesso nell'area riservata del soggetto in base ai dati di accesso (che per l'INPS sono il codice fiscale e un PIN rilasciato dall'INPS).

    ciao

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2018-06-29T20:37:49+00:00

    Ciao,

    io non riesco a farlo girare neanche per eseguire il login su facebook ad esempio.

    Dove sbaglio?

    grazie ancora

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2018-06-25T15:54:27+00:00

    Ciao a parte questa istruzione che dà errore

    oBrowser.timeout = 60

    ho provato il codice eseguendo l'accesso su un sito e viene eseguito correttamente.

    ciao

    La risposta è stata utile?

    0 commenti Nessun commento