Starting JavaScript query in IE from VBA - vba

I am trying to develop a macro that will be able to automatically check the format of tracking number in a sheet, decide which courier site to use, and then get the status of shipment. Until now it's going pretty well, since both tnt and dhl's results have tracking no in it's address, but I got kinda stuck with ups, where the submission of form seems to be done via javascript.
I managed to trick it by focusing on input box and making my macro send keys tab, tab and enter after inputting no, but it's not so elegant and the success rate seems to be about 70% from my testing.
I tried all different variations of getelementsby..., but nothing really seemed to submit the form successfully.
Is there a way to call the JavaScript to start query, or maybe another way of submitting the form itself?
Added an edited chunk of code for testing as well as html for both what seems to be code for the button and for the JavaScript :
sub ups()
Dim objIE
Dim Website
Dim Element
Dim cRng As Range
Set cRng = "1Z30RY119130505965"
Set objIE = "https://www.ups.com/tracking/tracking.html"
With objIE
.visible = True
.navigate Website
Do While objIE.Busy Or objIE.ReadyState <> 4
DoEvents
Loop
Set Element = .Document.GetElementsByName("trackNums")
'a part of code I found useful for when previous Do
'While fails and events start executing too early
Dim btimer As Integer
btimer = 0
Do While Element.Item(3).Value = 0
Application.Wait (Now + TimeValue("0:00:01"))
btimer = 4 Then
Exit Do
End If
Loop
Element.Item(3).Focus
Element.Item(3).Value = cRng.Value
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{~}"
<div class="btnBar">
<input name="track.x" class="ups-cta ups-cta_primary" type="submit" value="Track"></input>
</div>
<script language="JavaScript1.2" type="text/javascript">
$(function)
{
Tracking.setLocaleString('en_KR');
Tracking.setLoadingText('Loading. Please wait a moment.');
Tracking.setErrorMessage3('The systen is unable to process your request at this time. Please try again later.');
Tracking.addInatructionTextForyinputPage("Enter up to 25 tracking or InfoNotice numbers, one per line.", 'y');
Thanks for any ideas in advance.

Related

Checking if HTML elements exists after a input with VBA does not work correctly using html.getElementById()

I am pretty new to VBA and don't have much experience with it.
I have a excel sheet with more then 500 commands for which I want to test if the syntax of the command is correct. To do so I thought about using VBA to automate this process.
For the start I went with just one command to see if it could work, but it does not. My code:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://www.freeformatter.com/java-regex-tester.html"
IE.Navigate URL
While IE.ReadyState <> 4
DoEvents
Wend
IE.Document.getElementById("regex").Value = "\b(Yes"
IE.Document.getElementById("input").Value = "Yaes"
IE.Document.getElementById("matchesButton").Click
While IE.ReadyState <> 4
DoEvents
Wend
Set html = IE.Document
If IsNull(html.getElementById("output")) Then
Worksheets(12).Range("A2").Interior.ColorIndex = 3
End If
Set IE = Nothing
The input "\b(Yes" should be with syntax errors, so there should be this HTML code:
<div id="output" class="form-wrapper" style="margin-top:20px;padding-left:20px;">
But checking this id="ouptut" does not work, with this error there should no <div id="output" be and therefore IsNull(html.getElementById("output")) should return a Null/True and the excel cell should be marked red.
Can anyone please help me out with my logic error in the code? I tried already hours and should be simple, but I was not able to find a solution via google.
From this post here, you need to check if the object exists rather than if the element is null.
Create the object first
set Element = html.getElementByID("output")
Then run your check to see if the object exists
if isObject(Element) then
Try that.

VBA Automate IE: element.click and element.FireEvent (OnClick) Not working

I am trying to parse a web based pivot table.
Unfortunately, I need to expand additional lines in the web based pivot table in order to access all the data I need. I think that when I click on the + sign to expand additional lines, a script downloads the additional data from the database.
Anyway, here is the html code of the + sign:
<DIV class="clip9x9 di-img" onclick=_Ewa.Gim.tpdfce(event);>
<INPUT title=Expand class=ewaboot_expand type=image alt=Expand src="http://...">
</DIV>
I manage to get the correct element into an variable using IE.document.getElementsByTagName("Div") and a If Loop (for some reason the GetElementByClassName would crash my IE), but none of the .Click or .FireEvent (OnClick) methods works. I also tried both methods on the Input element below the DIV, and these are not working either.
Here is the code
Sub Test()
Dim IE As InternetExplorer
Dim div As Variant
Dim Obj As Variant
Dim WebUrl As String
WebUrl = "http://..../filename.aspx"
Set IE = New InternetExplorerMedium
Sleep 1000
IE.Visible = True
IE.navigate WebUrl
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
Sleep 1000
Loop
Set div = IE.document.getElementsByTagName("Div")
For Each Obj In div
If Obj.className = "clip9x9 di-img" Then
Obj.Click
Obj.FireEvent ("OnClick")
End If
Next i
End Sub
When I look at the onclick attribute of my Obj element in th Spy Window (VBA editor), the onclick property has a + sign aside, and when inspecting inside it, no variable is attributed to it. I find it weird since the HTML code indicates a value =”_Ewa.Gim.tpdfce(event);” which I believe refers to the script that expands the pivot table line.
Any help would be much appreciated

Unable to set timeout option within my scraper to save it from infinite looping

I've written a script in vba using IE to initiate a search in a webpage in it's searchbox to populate the result according to the search by hitting the search button. The webpage loads it's searchbox few seconds later it is made to open. However, my below script can handle this barrier and perform the search in the right way.
Now, I've got a slightly different question which is: suppose I've defined a wrong ID name or There is no such ID in that webpage then the way I've written my script will loop forever. This is certainly a serious issue.
How can I modify my script putting some timeout option within the loop so that the scraper will wait sometime for the element to appear and if the element is not present within that time then it will break out of the loop and quit the browser. Thanks in advance.
Here is my script:
Sub GetItems()
Dim HTML As HTMLDocument, post As Object
With New InternetExplorer
.Visible = True
.navigate "https://torrentz2.eu/"
While .Busy Or .ReadyState < 4: DoEvents: Wend
Set HTML = .Document
With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
DoEvents
Loop While post Is Nothing
post.innerText = "Udemy"
.getElementById("thesearchbutton").Click
End With
.Quit
End With
End Sub
Try replacing:
With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
DoEvents
Loop While post Is Nothing
post.innerText = "Udemy"
.getElementById("thesearchbutton").Click
End With
with
Dim dTime as Single ' as pointed out by #Mathieu Guindon in comments
'____________________________
dTime = Timer
With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
If Timer - dTime > 60 Then Exit Do ' 60: num of seconds, equal 1 min
DoEvents
Loop While post Is Nothing
post.innerText = "Udemy"
.getElementById("thesearchbutton").Click
End With

Return Address from Google term search to excel using VBA

I am familiar with StackOverflow but have just recently signed up. I am trying to search a Hotel on google and return the address in Excel using VBA. Below is a photo of what Information I am trying to return from Google. From my research, I was able to find a VBA that allowed me to return the Results stats.
Would it be possible to modify my code and return the box at the top of my google search?
I would really appreciate your help! Below is the VBA I am using to return search results.
Sample Image - Red Roof Inn & Address
Sub SearchGoogle()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
Dim var1 As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "http://www.google.co.in"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("lst-ib").Value = var
'Here we are clicking on search Button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
ie.Quit
Set ie = Nothing
Next x
End Sub
Right now your code loads the page and then loads the value of the resultStats element.
So the section of your code that you will need to alter is:
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
The first step to your problem is to understand the DOM of the HTML page you are attempting to use, in this case Google. I would suggest using a browser to navigate the DOM as it would give you a good idea of what the whole page is doing.
If you are aiming to do this on a macro basis you will need a path through the DOM that will always take you where you want to go. I would suggest having two pages with different searches open so that you can check you hypothesis as you go.
For example the boxes that you refer to seem to be located in a class called kp-header from knowing this you can build out your path through the DOM to return the text value displayed on screen. Again you will need to do your own investigations to find the best stating point for your search as kp-header was just the first potently helpful result I could find.
Although please note that depending on the speed you are loading these webpages you may hit a limit from google as they discourage scraping. What would be a better option to avoid these limits and to avoid yourself having to investigate all of google's DOM would be to try and incorporate one of google's API's

Importing specific web data to excel using VBA

I'm very much beginner to the VBA coding scene (web scripting is more my thing) but I have an excel based program I need to create that will import data from a intranet web based application into a spreadsheet. Here's the gist of what I'm looking to set up...
In the spreadsheet the user will enter the following info: username, password, list of customer account numbers and a date range. The user will then click a "command button" that will make the following happen:
Open web based program, login (based on login/password typed into spreadsheet) and navigate to the account search screen.
Enter first customer account number into search field and click the "search" button to navigate to the specific customer account.
Navigate to the "search activity" screen, enter the date range and click the "search activity button.
Pull the data from a specific column of the activity table and import the data to the spreadsheet.
If there are multiple pages of data there will be a "Next Results" button, there should be a loop to click the next results button (if it exists) and pull the same column of data from each page until the button no longer exists (no more data).
Once there are no more pages of data (or if there is only one page) the macro will loop back and navigate to the account search screen and perform the same operations for each account in the list of accounts typed into the spreadsheet until there are no other accounts.
Once completed (all data successfully imported to the spreadsheet) it should close the IE window.
It's a little complicated and I realize excel/vba is definitely not the best solution for performing these functions but unfortunately it's what I have to use in this instance. I've been able to piece together some VBA that does almost everything above, the problem I'm having is looping through the activity pages and pulling the data just will not work (get a wide range of errors that only confuse me more), sometimes it will pull data from the first sheet, click the "next results" button, get to the next page and throw an error or even get through two or three pages and throw an error. It doesn't make a lot of sense but the most common error is "permission denied". Also this code currently only pulls the data from one account, I was hoping once I got it working for one account it would be simple to create a loop of the entire code to have it go down the list of account numbers and do the same for each until completed. I've been stuck on this for a number of weeks and I'm really ready to toss out the whole thing and start from scratch, any help would be very very appreciated!
Below is the code I have so far...
Private Sub CommandButton1_Click()
' open IE, navigate to the desired page and loop until fully loaded
Set IE = New InternetExplorerMedium
my_url = "https://customerinfo/pages/login.jsp"
my_url2 = "https://customerinfo/pages/searchCustomer.jsp"
my_url3 = "https://customerinfo/pages/searchAccountActivity.jsp"
With IE
.Visible = True
.navigate my_url
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
End With
' Input the userid and password
IE.document.getElementById("userId").Value = [B2]
IE.document.getElementById("password").Value = [B3]
' Click the "Login" button
IE.document.getElementById("action").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
' Navigate to Search screen
With IE
.navigate my_url2
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
End With
' Input the account number & click search
IE.document.getElementById("accountNumber").Value = [B5]
IE.document.getElementById("action").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
With IE
.navigate my_url3
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
End With
'Input search criteria
IE.document.getElementById("store").Value = [C7]
IE.document.getElementById("dateFromMonth").Value = [C10]
IE.document.getElementById("dateFromDay").Value = [B11]
IE.document.getElementById("dateFromYear").Value = [B12]
IE.document.getElementById("timeFromHour").Value = [B20]
IE.document.getElementById("timeFromMinute").Value = [B21]
IE.document.getElementById("dateToMonth").Value = [C15]
IE.document.getElementById("dateToDay").Value = [B16]
IE.document.getElementById("dateToYear").Value = [B17]
IE.document.getElementById("timeToHour").Value = [B24]
IE.document.getElementById("timeToMinute").Value = [B25]
IE.document.getElementById("action").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
'Pulls data from activity search
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long, i As Long
Dim e As Object
Application.Wait Now + TimeValue("00:00:05")
Set TDelements = IE.document.getElementsByTagName("tr")
r = 0
For i = 1 To 1
Application.Wait Now + TimeValue("00:00:03")
For Each TDelement In TDelements
If TDelement.className = "searchActivityResultsOldContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
ElseIf TDelement.className = "searchActivityResultsNewContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
End If
Next
Application.Wait Now + TimeValue("00:00:02")
Set elems = IE.document.getElementsByTagName("input")
For Each e In elems
If e.Value = "Next Results" Then
e.Click
i = 0
Exit For
End If
Next e
Next i
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
IE.Quit
End Sub
So, what is happening after you've clicked on "Next..." element? Let me describe an issue I encountered. Assume the code flow as follows:
Create IE instance, and navigate to some URL, e. g. first search results page.
Make a check if the page is loaded and ready. Wait for it.
Create the DispHTMLElementCollection collection of the target elements, retrieved by .document.getElementsByTagName(), etc..
Loop through the elements of the collection, do some stuff.
Click on the "Next ..." element. The issue is that in some cases the next page doesn't start downloading immediately after click due to some JS or XHR processing.
Make a conventional check if the next page is loaded and ready. This check just allows the further code execution without any delay, since downloading of the next page has not been started immediately after click, and the current existing page is determined as next page downloaded and ready, by mistake. Simple several secs delays doesn't provide reliable way to get the ready page.
Again, create the DispHTMLElementCollection collection of the elements from the existing page, instead of the next page, by mistake.
Loop through the elements of the created collection. While the loop in progress, the next page starts downloading. The collection still contains the references to the objects, but actually the page with that objects has been unloaded. Thereby either attempt to access to the element of the unloaded page or due to document object is unresponsive, the operation gives "permission denied" errors.
My clue is to avoid clicking on "Next...", try to read the next page URL from .href property of the "Next..." anchor <a> element, and invoke IE.navigate to that URL, then check the page readiness.
Take a look at the example implementing that approach.
IMO the most efficient way is to use XHR, like this, this and this.