VB Click Button Macro Note Working - excel

I am trying to get the results of tracking a package on my screen. In Excel, I have this code, which successfully opens the IE browser and successfully types the tracking number into the input box. What doesn't work is the last step - button click. I get the runtime error 91 - object variable or with block variable not set. Thanks in advance.
Sub TrackUSPS()
Const cURL = "https://tools.usps.com/go/TrackConfirmAction!input.action" 'Enter the web address here
Const trackNum = "9405511899560005266920" 'Tracking number
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim TrackInputBox As HTMLInputElement
Dim TrackButton 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
'Get the tracking form on the page
Set LoginForm = doc.forms(1)
'Get the tracking input box and populate it
'<input type="text" id="search-text" autocomplete="off" name="searchText" class="default" value="Search USPS.com or Track Packages" onclick="javascript:dojo.byId('search-text').value='';"/>
Set TrackInputBox = LoginForm.elements("search-text")
TrackInputBox.Value = trackNum
'Get the form input button and click it
'<input type="image" id="search-btn" src="/media/images/global/blank.gif" alt="Process Search" />
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub

This works for me, replace
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
with
IE.document.getelementbyID("search-btn").Click

Related

Zillow web scraping in excel vba (Big issue help)

I am trying to web scrape Zillow. I am currently using web automation however, I cannot search my desire location. The value appears on the search bar, but when it submits it doesn't change it goes back to the same page. It is like the value pre-established stays there even though I change it. Please help me, I've been trying for so many days and I can't get answers.
Zillow's code.----------------------------------------------------------------------------------
<input class="react-autosuggest__input" role="combobox" aria-expanded="false" aria-controls="react-autowhatever-1" aria-owns="react-autowhatever-1" aria-autocomplete="list" aria-label="Search: Suggestions appear below" type="text" placeholder="Address, neighborhood, or ZIP" value="new jersey" autoComplete="off">
Sub zillow()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim zillowinput As MSHTML.IHTMLElementCollection
Dim zillowinput2 As MSHTML.IHTMLElementCollection
Dim direc As String
Dim iny As MSHTML.IHTMLElementCollection
Dim inys As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "https://www.zillow.com/homes/new-jersey_rb/"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop`enter code here`
Set doc = ie.document
direc = Range("D5").Value
Application.Wait Now() + #12:00:02 AM#
Set inys = doc.getElementById("srp-search-box")
Set inys = doc.getElementsByTagName("input")(0)
inys.Focus
inys.Value = "35 Krakow St, Garfield, NJ 07026"
inys.Blur
**strong text**
doc.forms(0).submit
The change event in the search box can be fired with SendKeys. You can simulate user input using SendKeys to set value of the search box and press Enter to do the search.
You can refer to the working code below:
Sub zillow()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim inys As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "https://www.zillow.com/homes/new-jersey_rb/"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
Set doc = ie.document
Application.Wait Now() + #12:00:02 AM#
Set inys = doc.getElementsByTagName("input")(0)
inys.Focus
SendKeys ("35 Krakow St, Garfield, NJ 07026")
Application.Wait (Now + TimeValue("00:00:02"))
SendKeys ("{ENTER}")
End Sub
Result in IE:

VBA Excel click on href and fill in form

im trying to get some data from a website by using vba.
The data i want is from this site: https://www.uitvoeringarbeidsvoorwaardenwetgeving.nl/mozard/!suite16.scherm1168?mSel=145576
What i want the code to do is click on the purple bar with the pencil
on it so the screen appears for filters and than fill in a specific time frame in the filters.
When this is done i want to get the data that appears.
Im able to navigate to the site and click on the purple bar so the filter screen appears. but i cant fill in the dates
this is the code i have so far:
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
Dim pastDate As MSHTML.IHTMLElement
Dim futuredate As MSHTML.IHTMLElement
IE.Visible = True
IE.Navigate "https://www.uitvoeringarbeidsvoorwaardenwetgeving.nl/mozard/!suite16.scherm1168?mGmr=66"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLAs = HTMLDoc.getElementsByTagName("a")
For Each HTMLA In HTMLAs
'Debug.Print HTMLA.className, HTMLA.getAttribute("href"), HTMLA.getAttribute("rel"), HTMLA.innerText
If HTMLA.getAttribute("href") = "https://www.uitvoeringarbeidsvoorwaardenwetgeving.nl/mozard/!suite16.scherm1168?mGmr=66#editmodal" Then
HTMLA.Click
Exit For
End If
Next HTMLA
Do While IE.ReadyState <> 4 Or IE.Busy:
DoEvents: Loop
Set HTMLInput = HTMLDoc.getElementById("frm_FKMT_B931_542_823883_dva_id1")
HTMLInput.Value = "01-01-2020" 'THIS GIVES AN ERROR?
The last line of code gives an error and i dont understand why??
This is the HTML code from the website that i want to change the value of:
<input name="FKMT_B931_542_823883_dva" class="datumveld form-control" id="frm_FKMT_B931_542_823883_dva_id1" type="text" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}">
Thanks and sorry for the inconvience or poorly asked question, if there is anything else you guys need to now please feel free to ask!
Thank you!!
This is an example to fill the first date field. The IDs seems not very stable.
Beware: There is a pattern for the entered dates
pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}"
There are some html events. I don't know if it is necessary to trigger them to make the dialog realy work.
Have you checked if the page works in IE?
Sub OpenAndFillForm()
Dim browser As Object
Dim url As String
Dim nodeToClick As Object
Dim nodeForm As Object
Dim nodeFirstDate As Object
url = "https://www.uitvoeringarbeidsvoorwaardenwetgeving.nl/mozard/!suite16.scherm1168?mGmr=66"
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
Do Until browser.readyState = 4: DoEvents: Loop
Set nodeToClick = browser.document.getElementByID("tabel2").getElementsByTagName("a")(0)
nodeToClick.Click
Application.Wait Now + TimeValue("00:00:02")
Set nodeForm = browser.document.getElementByID("tabel12")
Set nodeFirstDate = nodeForm.getElementsByClassName("datumveld")(0)
nodeFirstDate.Value = "31-12-2019"
End Sub

Automated Login Will Not Enter Credentials-Prompts Error

I'm encountering an error when trying to automate the login process for Kronos. I'm able to get to the site, but it stops short of entering in my credentials.
Have looked thru old code that I have for reference, and tried to find a solution on Stack but have been unable to. I Referenced MS HTML Object Library, MS Internet Controls, & WebServices 1.0 Type Library.
Sub LoginToKronos()
Const cURL = "https://wikainstruments-sso.prd.mykronos.com/"
Const cUserID = "firstname.lastname#business.com"
Const cPassword = "Password123"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInputBox As HTMLInputElement
Dim SubmitButton As HTMLInputButtonElement
Dim PasswordInputBox As HTMLInputElement
Dim LogOnButton As HTMLInputButtonElement
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
'Get the only form on the page
Set LoginForm = doc.forms(0)
'Get the Username textbox and populate it
'input name="loginfmt" id="i0116" value="" class="form-control ltr_override" type="email"
Set UsernameInputBox = doc.getElementById("i0116")
UsernameInputBox.Value = cUserID
'Get the form input button and click it
Set SubmitButton = doc.getElementById("idSIButton9")
SubmitButton.Click
'Get the Password textbox and populate it
'input name="Password" id="passwordInput" class="text fullWidth" type="password"
Set PasswordInputBox = doc.getElementById("passwordInput")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
Set LogOnButton = doc.getElementById("idSIButton9")
LogOnButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
'avoid any pop up
End Sub
would be nice to get have the credentials populate.

VBA Automated Login: URL Changes After First Use Resulting in Error

Code posted below. After alot of trying I managed to scrape together a macro that it will auto-log me into my bank account. If I try to run it a second time, the URL changes on me and I get an error message. If I clear my browsing history and close/re-open the file, it works again. I've tried setting my Internet Options to "Delete Browsing History on Exit". I also tried changing "Dim IE As InternetExplorer" to"Dim IE As InternetExplorerMedium" because I read somewhere that that might help. Something having to do with this being a secure site and it was redirecting me to a parent URL. Does anyone know what could be causing this and how I can fix it? I've asked around on the other forums but have had no luck with a response. Would greatly appreciate some help. Tks.
Note: I'm being redirected from https://chaseonline.chase.com/ to https://www.chase.com/
Have searched the internet looking for solutions. Have tried to get help on other forums but have been unsuccessful. Have tried fixing it myself but since I don't know what is the root cause, I don't know what to fix.
Sub Chase()
Const cURL = "https://chaseonline.chase.com/"
Const cUserID = "XXXXXX"
Const cPassword = "XXXXXX"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim LogOnButton 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
'Get the only form on the page
Set LoginForm = doc.forms(0)
'Get the Username textbox and populate it
'input name="UserID" id="UserID" size="18" value="" class="user-name" type="text"
Set UsernameInputBox = doc.getElementById("UserID")
UsernameInputBox.Value = cUserID
'Get the Password textbox and populate it
'input name="Password" id="Password" size="18" class="user-password" type="password"
Set PasswordInputBox = doc.getElementById("Password")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
Set LogOnButton = doc.getElementById("logon")
LogOnButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
success

VBA - IE automation - .getelements

I'm trying to enter text into an input field that has no ID. When I inspect the element I get this:
<input title="Affected Customer(s)" class="custom-input ng-pristine ng-invalid ng-invalid-required ng-touched" role="combobox" aria-expanded="false" aria-required="true" aria-owns="typeahead-78-1606" aria-autocomplete="list" required="" type="text" placeholder="Enter customer name, email, login ID or corporate ID" entity="incident" field-name="customer.loginId" potentially-required-field="" auto-focus="" typeahead="user as user.firstName + ' ' + user.lastName for user in getList('person', $viewValue)" typeahead-loading="person.isLoading" typeahead-wait-ms="500" typeahead-on-select="onCustomerSelect($model)" typeahead-template-url="views/create/custom-add-user-dropdown.html" typeahead-min-length="3" prevent-click-event="" ng-model="person.ngModel">
how do I reference this input field? I have tried many ways but they all fail. I think my understanding is lacking and hopefully a bit of help will get me on the right track. The code I have now that tries to locate this input field is:
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://ewpg-app-1041.hydro.mb.ca/ux/smart-it/#/create/incident"
'Navigate to URL
IE.navigate URL
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"
'click create new
'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.Document.getElementsByName("Affected Customer(s)")
'...get the href link and print it to the sheet in col C, row y
result = aEle
'repeat times the # of ele's we have in the collection
Next
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
The line "objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"" gives me the error Run-time error 424 - object expected.
Thank you for any help you can provide, this will probably be extremely easy but I'm just not understanding something basic obviously. The code above was taken from a various sites, seems to work fine. I had the for look working for testing using different logic.
Try Below Code instead :
getElementsByClassName("custom-input ng-pristine ng-invalid ng-invalid-required ng-touched")

Resources