VBA to automate IE website login

eastrand

Board Regular
Joined
Nov 27, 2013
Messages
101
Office Version
  1. 365
Platform
  1. Windows
I know this questions has already been asked multiple times but unfortunately, there are a lot of different ways to code a website. I've tried several different ways to automate this and just can't get it to work. I'm trying to automate my login to https://3plservices.pharmarxorder.com/login using the below code. When I manually type in the username and password and then click on "Sign In" it works. When I use the below code, it appears to fill in both the username and password correctly and clicks the sign in but unfortunately, it just reloads the login page. I appreciate any help.

VBA Code:
Sub test()
' open IE, navigate to the desired page and loop until fully loaded
    Set ie = CreateObject("InternetExplorer.Application")
    my_url = "https://3plservices.pharmarxorder.com/login"

    With ie
        .Visible = True
        .Navigate my_url

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    End With

' Input the userid and password

    ie.document.getElementByID("mat-input-0").Value = "UserName"
    ie.document.getElementByID("mat-input-1").Value = "PassWord"


' Click the "Search" button
    ie.document.forms(0).submit

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'm looking at the HTML of the website now. It looks like you're going to have to fool the website into thinking you're an actual human clicking and typing into the fields.

I'm no expert, but it's my understanding part of the human verification is the click events firing, so make sure to do the clicky (on some websites, clicking isn't enough... you have to fire the click events then click).

VBA Code:
Sub LoginToWebsite()
    Dim ie As New InternetExplorer
    ie.Visible = True
    
    'Bring ie window to front
    Call SetForegroundWindow(ie.hWnd)
    ie.navigate "https://3plservices.pharmarxorder.com/login"
    
    'Wait for website to load... the READY_STATE loop wasn't working for me.
    Application.Wait Now + TimeValue("0:00:05")
    
    Dim doc As HTMLDocument: Set doc = ie.document
    
    'To make sure you're sending keys to the right place...
    Call SetForegroundWindow(ie.hWnd)
    doc.getElementById("mat-input-0").Click
    SendKeys "TEST_USERNAME", True
    
    'Delay to let the browser process the keystrokes
    Application.Wait Now + TimeValue("0:00:01")
    doc.getElementById("mat-input-1").Click
    
    'Delay to let the browser process the keystrokes
    Call SetForegroundWindow(ie.hWnd)
    SendKeys "TEST_PASSWORD", True
    
    'Delay to let the browser process the keystrokes
    Application.Wait Now + TimeValue("0:00:01")
    
    'Click the button instead of submitting the form
    doc.getElementsByTagName("button")(0).Click
End Sub
 
Upvote 0
Solution
Also, I noticed the URL returned from the server after attempting to submit comes back with parameters for userName and password, so you might just be able to navigate to the URL with the username and password in those parameters. I don't know the security implications of doing that.

userName=test&password=pass
 
Upvote 0
@Strithken This worked great. I had to add another Application.Wait for 1 second after each click into the UN and PW fields. Also, I'm not sure what the calls to the 'SetForegroundWindow' are for but I commented out and it works. Thank you greatly for the help.
 
Upvote 0
@Strithken This worked great. I had to add another Application.Wait for 1 second after each click into the UN and PW fields. Also, I'm not sure what the calls to the 'SetForegroundWindow' are for but I commented out and it works. Thank you greatly for the help.
Glad I could help! And sorry about that... I forgot to include the SetForegroundWindow Function at the top. It's to bring the IE window to the front to make sure SendKeys isn't sending keys to the wrong application. Here's the function to include above the Sub for if you decide to use it in the future:
VBA Code:
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" _
    (ByVal hWnd As LongPtr) As LongPtr
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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