In some cases, it is not possible to effectively engage with elements on a website in Internet Explorer with the standard targeting technology of Foxtrot. Or, in some cases you simply aim to increase speed or precision. In such scenarios, the VBScript action comes in handy as you can write custom code to perform actions in websites.
- For general information on the VBScript action in Foxtrot, please read this article.
In this article, we will explain some of the core concepts when using the VBScript action to interact with IE. Whether you decide to write one long VBScript action to perform all you need to do or break it out in several actions is up to you. Remember, for every VBScript action, you need to properly connect to the IE window before performing the actions and always remember to clear the objects of the script in the end of the VBScript action.
Getting started
First of all, you need to either connect to an already opened window of IE or open a new IE window. Usually, it is relevant to connecto an already opened window of IE as you will use a VBScript action in the middle of a process in order to perform some actions that are not achievable via Foxtrot.
Connect to an opened window
For this example, it will only work if you have only one window of IE opened. If you have multiple windows opened, you need additional code to properly connect to the desired window, making it a bit more complicated.
'Dim the primary objects.
Dim objApp
Dim objIE
Dim objWindow
'Set the primary objects.
Set objApp = CreateObject("Shell.Application")
Set objIE = Nothing
'Identify the IE window and connect.
For Each objWindow In objApp.Windows
If (InStr(objWindow.Name, "Internet Explorer")) Then
Set objIE = objWindow
Exit For
End If
Next
With objIE
.Visible = True 'Make sure to set the window of IE to visible.
Do While .Busy Or .readyState <> 4
'Do nothing, wait for the browser to load.
Loop
Do While .Document.ReadyState <> "complete"
'Do nothing, wait for the VBScript to load the document of the website.
Loop
'***THIS IS WHERE YOU DO SOMETHING***
End With
'Clear the objects.
On Error Resume Next
objApp = Nothing
objIE = Nothing
objWindow = Nothing
This piece of code will declare the three primary variable objects. Hereafter, it loops through all opened windows to find the first Internet Explorer window to connect to. After this code, you are ready to start performing the desired actions.
Open a new IE window
The below code shows how you open a new window of IE and navigate to a desired website.
'Dim the primary objects.
Dim objIE
'Set the primary objects.
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True 'Make sure to set the window of IE to visible.
.Navigate("https://www.google.com/") 'Navigate to the desired website.
Do While .Busy Or .readyState <> 4
'Do nothing, wait for the browser to load.
Loop
Do While .Document.ReadyState <> "complete"
'Do nothing, wait for the VBScript to load the document of the website.
Loop
'***THIS IS WHERE YOU DO SOMETHING***
End With
'Clear the objects.
On Error Resume Next
objIE = Nothing
Perform actions
This first example is a very simple one on the www.google.com website. These two lines will write something in the search field and then click to search.
.Document.getElementsByName("q").Item(0).Value = "Foxtrot RPA" 'Write something to search for.
.Document.getElementsByName("btnK").Item(0).Click 'Click to search.
Here is a full example. Carefully read the code below. This code combines the two methods above on how to connect to IE by first trying to find an already opened window of IE and create a new one if none is found. Also, notice how the code also waits for the website to be fully loaded both before and after performing the actual actions, and how it makes sure to clear the object variables by the end.
'Dim the primary objects.
Dim objApp
Dim objIE
Dim objWindow
'Set the primary objects.
Set objApp = CreateObject("Shell.Application")
Set objIE = Nothing
'Set the URL of the process.
Dim strURL
strURL = "https://www.google.com/"
'Identify the IE window and connect.
For Each objWindow In objApp.Windows
If (InStr(objWindow.Name, "Internet Explorer")) Then
If (objWindow.LocationURL = strURL) Then
Set objIE = objWindow
Exit For
End If
End If
Next
'Open IE if not connected.
If (objIE Is Nothing) Then
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate(strURL)
End If
With objIE
.Visible = True 'Make sure to set the window of IE to visible.
Do While .Busy Or .readyState <> 4
'Do nothing, wait for the browser to load.
Loop
Do While .Document.ReadyState <> "complete"
'Do nothing, wait for the VBScript to load the document of the website.
Loop
'Perform some actions.
.Document.getElementsByName("q").Item(0).Value = "Foxtrot RPA" 'Write something to search for.
.Document.getElementsByName("btnK").Item(0).Click 'Click to search.
End With
'Clear the objects.
On Error Resume Next
objApp = Nothing
objIE = Nothing
objWindow = Nothing
Find Elements
For all of the following, you may reference this resource:
The hardest part is figuring out how to identify the element on the website that you need to engage with. There are many ways of doing this. The approach above uses the "getElementsByName" to find the relevant items to write a value and click a button. There are many ways of finding elements on a website. There is a significant difference between finding a single element or multiple elements. The above example uses "getElementsByName", meaning that it will find all elements with the specified name. In order to then specify the specific element to engage with, you then either need to loop through the elements or add ".Item(X)". If you, for example, know the specific ID of the element, you could use the "getElementById" method to find the specific element you need to engage with.
But how do you know how to find the correct element? In Internet Explorer, right-click on the element you wish to engage with and select the option "Inspect element".
Then we recommend that you activate DOM element highlighting to easier see what element you are inspecting.
Here is an example of how we could find the search textbox on the Google website with an alternative approach. First we find an appropriate parent element with a unique ID. This will make our identification process more precise. Hereafter, for the sake of the example, we then find the actual textbox using its class instead of name.
The code to write something in the search field would look like this.
'OLD LINE FROM ABOVE FULL SCRIPT USING THE NAME.
.Document.getElementsByName("q").Item(0).Value = "Foxtrot RPA" 'Write something to search for.
'NEW APPROACH.
.Document.getElementById("tsf").getElementsByClassName("gLFyf gsfi").Item(0).Value = "Foxtrot RPA" 'Write something to search for.
Additional examples
Here are additional examples to further inspire you.
Read text from website
This example is a simple addition to the above Google code. To read the number of search results after searching for "Foxtrot RPA", you need to add this code after the other action steps at the end of the with-statement. This will wait for the Google website to complete loading after searching and then display a message box with the number of search results.
Do While .Busy Or .readyState <> 4
'Do nothing, wait for the browser to load.
Loop
Do While .Document.ReadyState <> "complete"
'Do nothing, wait for the VBScript to load the document of the website.
Loop
Msgbox .Document.getElementById("resultStats").InnerText, vbInformation, "Result" 'Display a message box.
A message box is rarely relevant for actual processes, but they are really handy during testing. If you make sure to have a Foxtrot variable called "SearchResult" when executing the VBScript action, you can save the search result to this variable with this line of code instead of the message box code line above.
RPAEngine.SetVar "SearchResult", .Document.getElementById("resultStats").InnerText 'This will save the Google search result to the Foxtrot variable "SearchResult"
The natural questions would be:
- How do you know how to find the element?
- How do you know that you need the "InnerText"?
As shown already, to find the right way to find the element, inspect it in IE.
Next up is figuring out how to get the text of the element. Sometimes you need the ".Value", sometimes it is ".Text", in this case it is ".InnerText", and in some cases you need something else. The easiest way to figure this out is by targeting the element with Foxtrot and open the target details.
Complicated clicking
So far, all of above is something that you can easily achieve with the standard Foxtrot browser technology. This next example is a real case where Foxtrot was not able to effectively click on the desired button due to the structure of the website. The case is to click on a specific date, but the problem is the way that the website categorizes the dates. There are no way to set up simple rules to click the right date as the dates, for example the 20th, that button is the same for all months. Therefore, we need to use VBScript where we can first go into a specific month and then inside the specific month dictate the specific date. To achieve this, we also need some math to calculate different parameters as the website also includes blank spots for the week days before the month start.
The website url: https://booking.bluelagoon.com/calendar
See the code below and the attached Foxtrot project below the code for the full solution.
'Dim the primary objects.
Dim objApp
Dim objIE
Dim objWindow
'Set the primary objects.
Set objApp = CreateObject("Shell.Application")
Set objIE = Nothing
'Identify the IE window and connect.
For Each objWindow In objApp.Windows
If (InStr(objWindow.Name, "Internet Explorer")) Then
Set objIE = objWindow
Exit For
End If
Next
With objIE
.Visible = True 'Make sure to set the window of IE to visible.
Do While .Busy Or .readyState <> 4
'Do nothing, wait for the browser to load.
Loop
Do While .Document.ReadyState <> "complete"
'Do nothing, wait for the VBScript to load the document of the website.
Loop
'Retrieve the variable values.
Dim intMonth
Dim intWeekStart
Dim intDay
Dim intDayNumber
RPAEngine.GetVar "Month", intMonth
RPAEngine.GetVar "WeekStart", intWeekStart
RPAEngine.GetVar "Day", intDay
RPAEngine.GetVar "DayNumber", intDayNumber
'Perform the action.
If (intMonth = 0) Then
'Click on a date in the current month
.Document.getElementsByClassName("calendar-month").Item(0).getElementsByClassName("calendar-day-button").Item(intDayNumber).Click
Else
'Click on a date in a future month
intDay = intWeekStart + intDay - 2
.Document.getElementsByClassName("calendar-month").Item(intMonth).getElementsByClassName("calendar-day-button").Item(intDay).Click
End If
End With
'Clear the objects.
On Error Resume Next
objApp = Nothing
objIE = Nothing
objWindow = Nothing
See the Foxtrot project below for the full solution.
Comments
0 comments
Please sign in to leave a comment.