Log into Website Help ?

razzandy

Active Member
Joined
Jun 26, 2002
Messages
390
Office Version
  1. 2007
Platform
  1. Windows
The VBA code I am using is below and it runs without any errors BUT the button to log in doesn't actually get clicked.


The HTML Code on the website is here:

<form action="/Account/Login" id="loginForm" method="post" role="form"><input name="__RequestVerificationToken" type="hidden" value="BRqw7ES_53Qj1o9MZr-KpltdUyjNvKGaUQ4xjLdv8H7PVWPE82H6eyTanuOn-wXSi64TWDdaVGT3FDfO-dUNI0Gl3H41" /><div class="form-group">
<label class="control-label" for="Email">Email Address</label>
<input autocomplete="off" class="form-control" data-val="true" data-val-email="Please enter a valid email address." data-val-required="The Email Address field is required." id="Email" name="Email" type="text" value="" />
<span class="field-validation-valid text-primary" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label class="control-label" for="Password">Password</label>
<input class="form-control" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" placeholder="********" type="password" />
<span class="field-validation-valid text-primary" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
<button type="submit" class="btn btn-primary btn-block"><i class="fa fa-key"></i> Log in</button>
</form>

VBA Code:

Sub LogInToRoyalMail()

cURL = "https://Website here"
Const cUsername = "Username"
Const cPassword = "Password"

Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SignInButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate cURL

'Wait for initial page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

Set doc = IE.Document

Set LoginForm = doc.forms(0)

Set UserNameInputBox = LoginForm.elements("Email")
UserNameInputBox.Value = cUsername

Set PasswordInputBox = LoginForm.elements("Password")
PasswordInputBox.Value = cPassword

Set SignInButton = LoginForm.elements("__RequestVerificationToken")
SignInButton.Click

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

End Sub

Hope somebody can see where I'm going wrong.

Thanks in advance

:)
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I think it maybe something to do with the token part of the HTML, it seems to change with every visit to the website. So maybe once this part of my code runs:

Set SignInButton = LoginForm.elements("__RequestVerificationToken") SignInButton.Click

I then need to receive the token code then perform another action with the token to submit it to allow the login procedure on the website?

Anybody got any ideas?

;)
 
Upvote 0
Re your PM, try this:
Code:
    doc.getElementById("Email").Value = "user@useremail.com"
    doc.getElementById("Password").Value = "pass1234"    
    doc.getElementsByTagName("BUTTON")(0).Click
    
    With IE
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    End With
    
    MsgBox doc.body.innerText, Title:=IE.LocationURL
With the above credentials I get the expected error, "The username and password combination could not be verified...."
 
Upvote 0
Hi John

Can I kiss you? ;)

Where was I going wrong? Why did I not see the Tag Name BUTTON?

I am now working on the next screen after login. This screen has a pop up which asks to confirm a printer profile. I am giving it my best shot.

Following this page I have to click on a Hyperlink which when viewing element shows a shortened link, again I will have a go before I ask for more help.

Thank you so much again, this is great, thank you :)
 
Upvote 0
Hi Guys

Tried to navigate to the next page which is at: "https://proshipping.royalmail.com/Reports". To navigate to this I have to click a link called reporting. This link is part of an UL (Unordered List), a LI Tag. I simply tried: cURL = "https://proshipping.royalmail.com/Reports" but nothing happens.

HTML Code when inspecting the Reporting link is below:
Code:
<ul class="nav metismenu" id="side-menu">
    <li>
        <a href="/">
            <span class="nav-label">Dashboard</span>
        </a>
    </li>

        <li class="active">
            <a href="/Shipments/ShipmentProcessing">
                <span class="nav-label">Shipment Processing</span>
            </a>
        </li>

        <li class="  ">
            <a href="/Reports">
                <span class="nav-label">Reporting</span>
            </a>
        </li>

    <li class="pre-second-level ">
        <a href="/Maintenance">
            <span class="nav-label">Maintenance</span>
        </a>
        <ul class="nav nav-second-level              collapse" aria-expanded="false">
            <li>
                <a href="/Maintenance/AddressBook">
                    <i class="fa fa-angle-right"></i><span> Address Book</span>
                </a>
            </li>


            <li>
                <a href="/Maintenance/GoodsDescriptions">
                    <i class="fa fa-angle-right"></i><span> Goods Descriptions</span>
                </a>
            </li>


            <li>
                <a href="/Maintenance/PackageTypes">
                    <i class="fa fa-angle-right"></i><span> Package Types</span>
                </a>
            </li>


                <li>
                    <a href="/Maintenance/BulkMailingTemplates">
                        <i class="fa fa-angle-right"></i><span> Shipment Import Templates</span>
                    </a>
                </li>

                <li>
                    <a href="/Maintenance/SilentPrint">
                        <i class="fa fa-angle-right"></i><span> Silent Printing </span>
                    </a>
                </li>
            
        </ul>
    </li>
</ul>

Hope you can help

Thanks Again

Ry
 
Last edited:
Upvote 0
No Worries, after searching more I realised I needed to do this:

cURL = "https://webaddress"
IE.navigate cURL

Now I have one last Button to press, I will keep you posted ;)

Thanks Again John

Hi Guys

Tried to navigate to the next page which is at: "https://proshipping.royalmail.com/Reports". To navigate to this I have to click a link called reporting. This link is part of an UL (Unordered List), a LI Tag. I simply tried: cURL = "https://proshipping.royalmail.com/Reports" but nothing happens.

HTML Code when inspecting the Reporting link is below:
Code:
<ul class="nav metismenu" id="side-menu">
    <li>
        <a href="/">
            <span class="nav-label">Dashboard</span>
        </a>
    </li>

        <li class="active">
            <a href="/Shipments/ShipmentProcessing">
                <span class="nav-label">Shipment Processing</span>
            </a>
        </li>

        <li class="  ">
            <a href="/Reports">
                <span class="nav-label">Reporting</span>
            </a>
        </li>

    <li class="pre-second-level ">
        <a href="/Maintenance">
            <span class="nav-label">Maintenance</span>
        </a>
        <ul class="nav nav-second-level              collapse" aria-expanded="false">
            <li>
                <a href="/Maintenance/AddressBook">
                    <i class="fa fa-angle-right"></i><span> Address Book</span>
                </a>
            </li>


            <li>
                <a href="/Maintenance/GoodsDescriptions">
                    <i class="fa fa-angle-right"></i><span> Goods Descriptions</span>
                </a>
            </li>


            <li>
                <a href="/Maintenance/PackageTypes">
                    <i class="fa fa-angle-right"></i><span> Package Types</span>
                </a>
            </li>


                <li>
                    <a href="/Maintenance/BulkMailingTemplates">
                        <i class="fa fa-angle-right"></i><span> Shipment Import Templates</span>
                    </a>
                </li>

                <li>
                    <a href="/Maintenance/SilentPrint">
                        <i class="fa fa-angle-right"></i><span> Silent Printing </span>
                    </a>
                </li>
            
        </ul>
    </li>
</ul>

Hope you can help

Thanks Again

Ry
 
Last edited:
Upvote 0
That was my first thought - you need to Navigate to the URL!

In case the URL changes, this looks for the 'Reporting' link and clicks it:
Code:
    Dim reportingLink As HTMLAnchorElement, i As Long
    Set reportingLink = Nothing
    i = 0
    While i < doc.Links.Length And reportingLink Is Nothing
        If doc.Links(i).innerText = "Reporting" Then Set reportingLink = doc.Links(i)
        i = i + 1
    Wend
    If Not reportingLink Is Nothing Then
        reportingLink.Click
        'IE wait loop here
    Else
        MsgBox "Reporting link not found"
    End If
 
Upvote 0
Hi John

I am now stuck on clicking the button to generate the report. I just don't know where I'm going wrong? I have tried: doc.getElementsByTagName("button")(0).Click but it will not work I've also tried other tags names but nothing. The only one that gets no error is: doc.getElementsByTagName("button")(0).Click but it does not click it. HTML below.

Once the above problem works then my real problem starts because the Save As Dialogue pops up so I'm not sure how to deal with that!

Thanks Again John

:)

Rich (BB code):
<form class="form-horizontal" action="/Reports/DailyShippingReport" method="post" novalidate="novalidate" data-submittedtimeout="5000"><input name="__RequestVerificationToken" type="hidden" value="bHynmrRr1aC04X2Doxby4PbvYxf7qgSL6Ro9Y3Vh1UNLCE-3ul3xqUE7fbv8gMXRoWUVJ2LlSTSesHt2eIfz8ODf1Vk3WjF-JD78QIQG7mLbLvMLvYkGYt1XP8DIv_mLL2MtHg2">            <div class="row">
                <div class="col-xl-6 col-lg-8 col-md-10 col-sm-12">
                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="ShippingDate">Shipping Date</label>
                        <div class="col-sm-9">
                            <div class="input-group" style="max-width: 200px;">
                                <input name="ShippingDate" class="form-control hasDatepicker" id="ShippingDate" type="text" placeholder="select date..." value="22/09/2018" data-val-required="The Shipping Date field is required." data-val="true" data-val-date="The field Shipping Date must be a date.">
                                <span class="input-group-addon" style="cursor: pointer;" *******="$('#ShippingDate').focus();">
                                    <i class="fa fa-calendar"></i>
                                </span>
                            </div>
                            <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ShippingDate"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="GroupBy">Group By</label>
                        <div class="col-sm-9">
                            <select name="GroupBy" class="form-control focusOnMeFirst valid" id="GroupBy" aria-invalid="false" aria-required="true" aria-describedby="GroupBy-error" style="max-width: 300px;" data-val-required="The Group By field is required." data-val="true"><option value="0">Manifest Number</option>
<option value="1">Country</option>
<option value="2">Department</option>
<option selected="selected" value="3">Service</option>
<option value="4">Posting Location</option>
</select>
                            <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="GroupBy"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-9">
                            <button class="btn btn-primary btn-lg m-r-lg" type="submit">Create Report</button>
                            <a class="btn btn-default btn-lg" href="/Reports">Cancel</a>
                        </div>
                    </div>
                </div>
            </div>
</form>
 
Last edited:
Upvote 0
I have sussed out the clicking of the Generate Button with: doc.getElementsByClassName("btn btn-primary btn-lg m-r-lg").Item.Click

Now I really need help with the: 'Do you want to open or save this file' pop up. I've not got a clue with this! :(

I would like it to just go ahead and save the file to a directory of my choice.

Cheers

Ry


Hi John

I am now stuck on clicking the button to generate the report. I just don't know where I'm going wrong? I have tried: doc.getElementsByTagName("button")(0).Click but it will not work I've also tried other tags names but nothing. The only one that gets no error is: doc.getElementsByTagName("button")(0).Click but it does not click it. HTML below.

Once the above problem works then my real problem starts because the Save As Dialogue pops up so I'm not sure how to deal with that!

Thanks Again John

:)

Rich (BB code):
<form class="form-horizontal" action="https://www.mrexcel.com/Reports/DailyShippingReport" method="post" novalidate="novalidate" data-submittedtimeout="5000"><input name="__RequestVerificationToken" type="hidden" value="bHynmrRr1aC04X2Doxby4PbvYxf7qgSL6Ro9Y3Vh1UNLCE-3ul3xqUE7fbv8gMXRoWUVJ2LlSTSesHt2eIfz8ODf1Vk3WjF-JD78QIQG7mLbLvMLvYkGYt1XP8DIv_mLL2MtHg2">            <div class="row">
                <div class="col-xl-6 col-lg-8 col-md-10 col-sm-12">
                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="ShippingDate">Shipping Date</label>
                        <div class="col-sm-9">
                            <div class="input-group" style="max-width: 200px;">
                                <input name="ShippingDate" class="form-control hasDatepicker" id="ShippingDate" type="text" placeholder="select date..." value="22/09/2018" data-val-required="The Shipping Date field is required." data-val="true" data-val-date="The field Shipping Date must be a date.">
                                <span class="input-group-addon" style="cursor: pointer;" *******="$('#ShippingDate').focus();">
                                    <i class="fa fa-calendar"></i>
                                </span>
                            </div>
                            <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ShippingDate"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="GroupBy">Group By</label>
                        <div class="col-sm-9">
                            <select name="GroupBy" class="form-control focusOnMeFirst valid" id="GroupBy" aria-invalid="false" aria-required="true" aria-describedby="GroupBy-error" style="max-width: 300px;" data-val-required="The Group By field is required." data-val="true"><option value="0">Manifest Number</option>
<option value="1">Country</option>
<option value="2">Department</option>
<option selected="selected" value="3">Service</option>
<option value="4">Posting Location</option>
</select>
                            <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="GroupBy"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-9">
                            <button class="btn btn-primary btn-lg m-r-lg" type="submit">Create Report</button>
                            <a class="btn btn-default btn-lg" href="https://www.mrexcel.com/Reports">Cancel</a>
                        </div>
                    </div>
                </div>
            </div>
</form>
 
Last edited:
Upvote 0
You can't easily or reliably code around that. If this only needs to work on your machine, you could try using windows api calls, however depending on how you have internet explorer configured, this may prove very difficult.
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top