VBA website automation - vba

Good Afternoon All,
I have created some code that automatically logs me into my works website, however when trying to get the VBA to enter value into one of the search boxes I always recieve error 438: Object doesn't support this propery or method.
This is my code below please can you help:
Option Explicit
Const MyUserName As String = "username"
Const MyPassword As String = "password"
Const READYSTATE_COMPLETE As Integer = 4
Dim objIE As Object
Sub LoginScript()
Dim Message, Title, Default
Dim MyValue As String
Dim currenttime As Date
Set objIE = CreateObject("InternetExplorer.Application")
If MsgBox("Do you want to search for a member?", vbYesNo, "Login") =
vbNo Then
With objIE
.Visible = True
.Silent = True
.Navigate ("https://XXXXXXXXX.com/")
Do Until .ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
.Document.all.txtuserid.Value = MyUserName
.Document.all.txtpassword.Value = MyPassword
.Document.all.save_button.Click
Do Until .ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
End With
Else
Message = "Please enter clients CTC number" ' Set prompt.
Title = "Member Search" ' Set title.
' Display message, title, and default value.
MyValue = InputBox(Message, Title)
With objIE
.Visible = True
.Silent = True
.Navigate ("https://XXXXXXXXX.com/")
Do Until .ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
.Document.all.txtuserid.Value = MyUserName
.Document.all.txtpassword.Value = MyPassword
.Document.all.save_button.Click
Do Until .ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
currenttime = Now
Do Until currenttime + TimeValue("00:00:10") <= Now
Loop
Do Until .ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
.Document.getElementsById("txtClientRef").Value = MyValue <------ Errors Here
End With
End If
End Sub

Id should be unique, and the syntax is getElementById, so try:
.Document.getElementById("txtClientRef").Value
It is also a good idea to do .Document.getElementById("txtClientRef").Focus first.
If this then yields runtime error 424 it may be either that the object doesn't exist, as specified, or is not available at the time you attempt the assignment. For the latter to you can attempt to loop until not nothing with a specified timeout.

Related

How to take value from excel workbook to find something in a web form dropbox

I am working on a web automation project. I have come up to a web drop box to where I have to choose a value. I have a query selector which works fine in doing the job but now I want that query selector to find those values in the drop box which are taken from a cell value of excel sheet.
With .document.querySelector("[value='536']")
.Selected = True
.dispatchEvent evt
End With
Above is the query which works fine. But now I want to replace "536" from cell "A1" .
I have tried to replace ("[value='536']") with ([value='thisWorkbool.sheets.("sheet1").Range(A1).value']") but it shows error.
Option Explicit
Public Sub FillForm()
Dim ie As Object, tool As Workbook
Set tool = ActiveWorkbook
Set ie = CreateObject("InternetExplorer.application")
With ie
.Visible = True
.navigate "https://******.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.getElementById("txtUserName").Value = "09100107801-01"
.getElementById("txtPassword").Value = "Abc#1234"
.getElementById("btnSubmit").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
.navigate "https://***************"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim evt As Object
Set evt = .document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
With .document.querySelector("[value='536']")
.Selected = True
.dispatchEvent evt
End With
end sub
How to put the value from the cell value "A1" of exel sheet in above code.
Something like this should work:
Dim v
v = thisWorkbook.sheets("sheet1").Range("A1").value
With .document.querySelector("[value='" & v & "']")
.Selected = True
.dispatchEvent evt
End With

Unable to make my script stop printing wrong result

I've created a script in vba using IE to fill in few inputs in a webpage in order to reach a new page to check for some items availability based on inputting some values in an inputbox.
To walk you through: what the script is currently doing:
Select Buy Bricks from landing page
Enter age 30 and country United Kingdom and then click on submit button
On the next page, enter the unique identification number for the Lego piece in the Element/design number box to populate result.
My script can satisfy all the requirements stated above. However, when I try with three different numbers, as in 4219725,765467 and 230223 I can see that the one in the middle 765467 doesn't populate any result but It prints the result of it's earlier number.
All the three numbers have been used in a for loop within my script below.
How can I make my script print nothing when there is no result instead of printing wrong result?
Site address
My script so far: (could not kick out hardcoded delay)
Sub GetDetails()
Const timeOut = 10
Dim IE As New InternetExplorer, Html As HTMLDocument
Dim elem As Object, post As Object, inputNum As Variant
Dim ageInput As Object, itm As Object, T As Date
With IE
.Visible = True
.navigate "https://www.lego.com/en-gb/service/replacementparts"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
Dim event_onChange As Object
Set event_onChange = .document.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
Html.querySelectorAll(".arrow-list-info")(2).Click
Do: Set ageInput = Html.querySelector("input[id*='How old']"): DoEvents: Loop While ageInput Is Nothing
ageInput.innerText = 30
Html.querySelector("[label='United Kingdom").Selected = True
Html.querySelector("select").dispatchEvent event_onChange
Html.querySelector("[ng-click='startFlow()'").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
For Each inputNum In [{4219725,765467,230223}]
T = Timer
Do: Set post = Html.querySelector("[placeholder='Element/design number']"): DoEvents: Loop While post Is Nothing
post.ScrollIntoView
post.Focus
post.innerText = inputNum
Html.querySelector("button[ng-click='searchItemNumber()']").Click
'Can't kick out this hardcoded delay
Application.Wait Now + TimeValue("00:00:02")
Do
Set elem = Html.querySelector("div.list-item")
If Timer - T > timeOut Then Exit Do
DoEvents
Loop While elem Is Nothing
Set itm = Html.querySelector("h6.title")
If Not itm Is Nothing Then
Debug.Print itm.innerText
Else:
Debug.Print "Found Nothing"
End If
Next inputNum
Stop
End With
End Sub
So this needs tidying up but does it. I got rid of the explicit wait and added a wait for the spinner to disappear. For the no results section I look for an additional element to be present in the html when not found.
Option Explicit
Public Sub GetDetails()
Const timeOut = 10
Dim ie As New InternetExplorer, html As HTMLDocument
Dim elem As Object, post As Object, inputNum As Variant
Dim ageInput As Object, itm As Object, t As Date
With ie
.Visible = True
.navigate "https://www.lego.com/en-gb/service/replacementparts"
While .Busy Or .readyState < 4: DoEvents: Wend
Set html = .document
Dim event_onChange As Object
Set event_onChange = .document.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
html.querySelectorAll(".arrow-list-info")(2).Click
Do: Set ageInput = html.querySelector("input[id*='How old']"): DoEvents: Loop While ageInput Is Nothing
ageInput.innerText = 30
html.querySelector("[label='United Kingdom']").Selected = True
html.querySelector("select").dispatchEvent event_onChange
html.querySelector("[ng-click='startFlow()']").Click
While .Busy Or .readyState < 4: DoEvents: Wend
For Each inputNum In [{4219725,765467,230223}]
Do: Set post = .document.querySelector("[placeholder='Element/design number']"): DoEvents: Loop While post Is Nothing
post.Focus
post.innerText = inputNum
html.querySelector("button[ng-click='searchItemNumber()']").Click
Do
Loop While .document.querySelectorAll(".basic-search-btn .icon-spinner-arrows").Length > 0
t = Timer
Do
Set elem = html.querySelector("div.list-item")
If Timer - t > timeOut Then Exit Do
DoEvents
Loop While elem Is Nothing
Set elem = Nothing
Set itm = html.querySelector("h6.title")
If html.querySelectorAll(".alert.alert-info.margin-top.ng-hide").Length = 1 Then
Debug.Print "Found nothing"
Else
Debug.Print itm.innerText
End If
Set itm = Nothing
Next inputNum
ie.Quit
End With
End Sub

IE Login Automation with VBA failed for microsoft website

I have the following code, which works fine until it fills in my username and Microsoft Login Website seems to produce weird error and I have no idea how to go forward. See code and picture for better understanding of this issue.
Sub testLogin()
Dim IE, username, url, password
Set IE = CreateObject("InternetExplorer.Application")
username = "myusername#outlook.com"
url = "https://login.microsoftonline.com/"
password = "mypassword"
With IE
.Visible = True
.Navigate url
Do While .Busy
Sleep 100
Loop
' fill in username => this step produce weird input value
.Document.forms.f1.elements.loginfmt.Value = username
' click next
.Document.forms.f1.elements.idSIButton9.Click
.Document.forms.f1.elements.passwd.Value = password
End With
'clean up code
End Sub
Try this. It should get you there:
Sub Log_In()
Dim IE As New InternetExplorer, html As HTMLDocument
With IE
.Visible = True
.navigate "https://login.microsoftonline.com/"
While .readyState < 4: DoEvents: Wend
Set html = .document
End With
Application.Wait Now + TimeValue("00:00:05")
html.getElementsByName("loginfmt")(0).innerText = "your_email"
Application.Wait Now + TimeValue("00:00:03")
html.getElementById("idSIButton9").Click
Application.Wait Now + TimeValue("00:00:05")
html.getElementsByName("passwd")(0).innerText = "your_password"
Application.Wait Now + TimeValue("00:00:03")
html.getElementById("idSIButton9").Click
IE.Quit
End Sub

Not able to login to a webpage using excel, error 424 object required

I am trying to login to this webpage, https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp
using VBA. Debugging shows me that the VBA throws an error 424 object required when username line is active (apparently it is not able to fill the username data).
Here's the code:
Sub Test()
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
With ie.document
.getElementById("txtUserId").Value = "ABCDE"
.getElementById("txtPassword").Value = "ABCDE"
.getElementById("submit").Click
End With
End Sub
Can anyone help me with debugging the problem while logging in to the given webpage?
Take a look at the below example:
Option Explicit
Sub Test()
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.application")
With oIE
.Visible = True
.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
Do While .ReadyState < 4 Or .Busy
DoEvents
Loop
With .Document
Do While .ReadyState <> "complete"
DoEvents
Loop
With .parentWindow.frames("frmCUMain").document
.getElementsByName("txtUserId")(0).Value = "ABCDE"
.getElementsByName("txtPassword")(0).Value = "ABCDE"
.getElementsByName("cmdLogin")(0).Click
End With
End With
End With
End Sub

Press a link on a webpage using Excel VBA

I have this code where it goes to Minecraft.net and enters the username and password, and then goes to the profile page. I need to change the password on the account, which will be in cell A1 of the worksheet. I can't seem to figure out how to click the change password link. This is my code so far:
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' **********************************************************************
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
IE.Document.getElementById("signin").Click
'**********************************************************************
Application.StatusBar = "Form Submitted"
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
If the click doesn't work then you could try to submit the html form.
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
While IE.Busy
DoEvents
Wend
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = IE.Document.getElementById("loginForm")
htmlForm.submit