0

i try to use the VBA code found from Login to a website using VBA

unfortunately i cannot access it as VBA is prompting some error message.

error VBA received Run time error '91' Object variable or With Block variable not set

Sub login()

    Const Url$ = "https://kn.slync.app/login"

    Dim UserName As String
    Dim Password As String
    Dim LoginData As Worksheet

    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    
    UserName = LoginData.Cells(1, 2).Value
    Password = LoginData.Cells(2, 2).Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

    Dim oLoging As Object
    Dim oPassword As Object

    Set oLogin = .document.getElementsByName("username")(0)
    Set oPassword = .document.getElementsByName("password")(0)
    
        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit

    End With

End Sub


Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

Amendment made, Username and Password is populate as below screenshot enter image description here

This is the error message received on next Line .getElementsByClassName("ant-row-flex ant-row-flex-center ant-row-flex-middle")(0).Click

enter image description here

  • What would have helped is to include the relevant html as the problem is due to different html on the target page from that which the code you tried to adapt was working with. This should help you learn the basics: https://www.youtube.com/watch?v=UB1O30fR-EE&list=PLillGF-RfqbZTASqIqdvm1R5mLrQq79CU&ab_channel=TraversyMedia . Including the actual link was helpful. – QHarr Nov 01 '20 at 13:02
  • I try to open the site "https://kn.slync.app/login" directly in IE but it can't work in IE. Do you automate this site in IE or it's just a fake url? You can't automate it in IE if the site can't work with IE originally. If it's a fake url, I suggest that you can provide the real link so that we can analyze the html structure. – Yu Zhou Nov 02 '20 at 07:29
  • @YuZhou the site https://kn.slync.app/login is correct. – Zulkifle Rahmat Nov 03 '20 at 02:07
  • I add `Application.Wait (Now + TimeValue("00:00:05"))` after `.Visible = True` and `.getElementById("password").Value = Password`, the code runs well without errors. From your last comment, you say it shows "Username/Password is require", this message will show when the username or password is not correct. So I think your code is correct, maybe the username and password values you set are wrong. You can try to check if the username and password you use are valid. – Yu Zhou Nov 03 '20 at 09:05
  • @YuZhou I did the additional VBA as your ... Everything is OK. the only problem that i have now it won;t login and request the same Username/password . i wanted to paste screenshot of the error but i cannot find how to in these comment. – Zulkifle Rahmat Nov 04 '20 at 01:55
  • You can paste the screenshot in your original post by editing it. – Yu Zhou Nov 04 '20 at 02:06
  • @YuZhou ok thanks. I have insert on the Picture before and after with Error and The Code line. i really not sure why when the next code run, all first line 2 line value remove. similar error after i try the F8 and stop at the Password line by manually click the SignIn button – Zulkifle Rahmat Nov 04 '20 at 05:40
  • @ZulkifleRahmat I've added an answer, please check it. I think you can make it with `SendKeys`. – Yu Zhou Nov 04 '20 at 07:59

3 Answers3

1

The login input fields do not have the name attribute. You want to use the ids:

Set oLogin = ie.document.getElementById("username")
Set oPassword = ie.document.getElementById("password")

Make sure you have a long enough wait for page to load before attempting to set these elements as the page has a lot of dynamic content to load.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • I try to change the attribute but now is giving me an error "object doesn't support this property or method" – Zulkifle Rahmat Nov 01 '20 at 14:30
  • on which line please? – QHarr Nov 01 '20 at 14:45
  • Set oLogin = .document.getElementById("username") <-- the line that is prompting error – Zulkifle Rahmat Nov 02 '20 at 01:58
  • try either _Set oLogin = ie.document.getElementById("username")_ or _Set oLogin = ie.document.querySelector("#username")_ – QHarr Nov 02 '20 at 05:36
  • it the same error message received as Sebastian Zolg. Object Missing. i'm wondering if it has to do with the site itself... – Zulkifle Rahmat Nov 02 '20 at 06:10
  • I have only tested in console and it is there and working. Did you step through with F8 and see if error goes away? – QHarr Nov 02 '20 at 06:30
  • i try the step through and it is working fine except that it did not login as it suppose to.. but when i try to run it, The error message it there. does it mean the missing object is actually refering to the Login? – Zulkifle Rahmat Nov 02 '20 at 06:55
  • It means you need a [timed waiit](https://stackoverflow.com/a/55217080/6241235) for the page to load and username element to be present. Then, if details are entered but login doesn't occur you need to explore if certain htmlevents need to happen to trigger page recognising inputs have been given. – QHarr Nov 02 '20 at 07:04
0

Here is a quick working repro of your code with some simplification. I assume you want to set UserName and Password fields and then submit it.

As QHarr already indicated here, you have to use getElementById() instead of getElementsByName().

Sub login()

    Const Url$ = "https://kn.slync.app/login"

    Dim UserName As String
    Dim Password As String
    Dim LoginData As Worksheet

    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    
    UserName = LoginData.Cells(1, 2).Value
    Password = LoginData.Cells(2, 2).Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

        Set document = .document
        
        With document
            .getElementById("username").Value = UserName
            .getElementById("password").Value = Password
            .forms(0).submit
        End With
        
    End With

End Sub


Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub
Sebastian Zolg
  • 1,921
  • 2
  • 14
  • 35
  • Thanks. I did try to use but it came with the error message for Object require under .getbyelement when try to run it... separately, i give a try to disable the get element id, does not give any error message. does this conclude that the object must be assign for the username/password? – Zulkifle Rahmat Nov 02 '20 at 05:08
  • I created a blank new excel file/sheet and added my code from above (copy/paste). It works perfectly fine. Please try again, creating everything from scratch and just copy/paste the code. If it still not works, please update your initial question with detailed code snippets and screenshots of your error. – Sebastian Zolg Nov 02 '20 at 08:22
  • i did use the F8 function to run each line. although it does run, it didn't manage to login thou you have also include the Time wait. And when i click on the Run button, it just show message Object Require. im not sure if it the concern of the webpage itself. – Zulkifle Rahmat Nov 03 '20 at 02:06
  • i make some changes to the Last line With document .getElementById("username").Value = UserName .getElementById("password").Value = Password .getElementsByClassName("ant-row-flex ant-row-flex-center ant-row-flex-middle")(0).Click . it manage to activate the submit button. But somehow, the Username and Password were remove and is now prompting for "Username/Password is require" – Zulkifle Rahmat Nov 03 '20 at 03:34
0

I see what the issue you're facing now. Set value actually doesn't trigger the inputs as having values. The values of username and password were still considered empty. In this situation, we can use SendKeys to simulate user input then the events of the inputs will be triggered successfully.

You can change the code according to below:

With document
     .getElementById("username").Focus
     SendKeys (UserName)
     Application.Wait (Now + TimeValue("00:00:01"))
     .getElementById("password").Focus
     SendKeys (Password)
     Application.Wait (Now + TimeValue("00:00:01"))
     .getElementsByClassName("ant-row-flex ant-row-flex-center ant-row-flex-middle")(0).Click
End With

I test with a fake username/password and the message now is "Invalid username/password" which showing it works:

enter image description here

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22