Suresh9
ServiceNow Employee
ServiceNow Employee

Imagine you have a use case where you need to navigate through all the links available on a web page, for instance, searching for OnePlus mobiles on Amazon's website. You want to click on each link to capture detailed information. This article will guide you on how to accomplish this by capturing a single element and then iterating through them to perform actions on each link.

 

Suresh9_0-1698381913451.png

 

To start, we'll make use of the Universal Application Connector, which has been available since the Utah release. We'll use it to capture the necessary element – the first link. When capturing, you'll notice that only one element matches.

 

Suresh9_1-1698381913458.png

 

Now, let's modify our matching rules to find all mobile links on the screen. Change the Locator match rule to use the Tag Name and then select the class attribute as an additional match rule. This configuration instructs it to look for Anchor elements with the specified class. Refresh the element, and you'll see that more than one element matches, corresponding to the number of mobile links visible on the screen.

 

Suresh9_2-1698381913463.png

 

To determine the number of matching elements, expand the Universal app connector to see the screen and elements. Double-click on the element to explore the available methods in the Object Explorer, you would see GetInstanceCount. Now, let's design the automation as shown in the screenshot below:

 

Suresh9_3-1698381913468.png

 

GetInstanceCount method provides the count of matched elements. SetElementIndex method accepts an index as input, allowing you to perform actions on specific element. For example, if you pass the index as 2, it will execute actions on the third link. The default value is 0, which corresponds to the first link. Importantly, this method doesn't perform any action on the element itself; it merely sets the index for interaction. You can use this method even if the screen/element is not found on the machine, as it sets the index without raising errors, unlike other element action methods.

As you can see, we are iterating through each element (mobile link) to obtain the required information after navigating to the respective screen (referred to as "DoSomething" in this example). Notice that we use SetElementIndex before WaitForCreate; this is crucial for resetting the values. Failing to reset this value may result in elements not matching as expected. Thus, it is advisable to reset the values before actual usage.

By following these steps, you can efficiently perform actions on multiple elements of the same type, enhancing your automation capabilities for web scraping and other tasks. Happy automating!

1 Comment