Selectors are one of the major and most important topics when it comes to automation. Selectors are automatically created using standard XML code, which identifies the user interface elements. The XML code consists of several nodes, which is a combination of tags and attributes in a structured and hierarchical order.
In this chapter, you will learn what selectors are, how they work, and how to use the correct selector types and settings when automating. You will learn about the user interface, how to perform element exploration, and how to fine-tune selectors to improve the accuracy of element identification.
In this chapter, we will cover the following topics:
This chapter requires that you have UiPath Studio and UiPath Robot installed, along with a basic understanding of Studio's user interfaces. Please refer to Chapter 2, UiPath Ecosystem, to learn about the basics of Studio.
In this section, we'll learn more about how to use the tags and attributes of selectors. As we mentioned previously, selectors consist of a group of nodes of a specific element in a detailed hierarchical order. This is very similar to the address order we use for identification; that is, country > city > ZIP code > street name > street number > apartment number. The structure of selectors can be represented as follows:
<Node 1> will primarily be the parent node or root node; the last node, <Node n>, will be the one you are likely to be be interested in because it usually represents the UI element and the previous nodes are the parents of that element. Each node has attributes to help you identify the element in the user interface such as windows, tables, links, text boxes, buttons and so on and it's essential to understand the attributes of each node to apply constant values to accurately identify the correct element. Let's look at an example of such selectors:
In the <Node 1> code section, wnd (Window) is a tag and app='notepad.exe' cls='Notepad' title='Untitled – Notepad is an attribute. Similarly, in <Node 2>, Ctrl (Control) is a tag and name='Text Editor' role='editable text' is an attribute.
Now that you are aware of selectors and their usage, you need to how to edit them so that you can identify the target element based on your requirements. We will learn how to edit selectors in the next section.
Selectors are usually stored inside the Properties panel of the activities used in the workflow and can be accessed via Input > Target > Selector, in the Properties window as shown in the following screenshot:
The Selector Editor tool helps you view the selectors that have been used and edit their attributes to fine-tune the selector for better reliability. To access the tool, you can either click on the three dots in the Properties panel, as highlighted in the previous screenshot, or click on the Edit Selector option via the Options menu for any specific activity. In this case, the activity used in Type Into, as shown in the following screenshot:
A new window will appear, where you have multiple options. Each has significance in terms of overcoming any selector challenges. Let's explore these options one by one; they are shown in the following screenshot:
The various options are as follows:
The Validate button displays the status of the selector based on the availability of the target UI element that's displayed on the screen. It has three different states:
This is used to select the new UI targets or to overwrite the previously selected targets on the screen.
This button is enabled when the selector is invalid and is used to repair the existing selector by indicating the same target UI element on the screen.
This button is used to ensure that the modified selector is still valid for the target element. Once clicked, the target element will be highlighted on the screen.
This option lists all the target element attribute's tag names and attribute values.
This box contains the XML fragment that provides the tag names and attribute values of the target element. These can be edited to help fine-tune the selector.
This is a separate tool that can be used to refine the selector options and is only displayed for valid selectors.
In this section, we looked at the various options within the Selector Editor and how to manipulate selectors and attributes using the basic options provided. For more advanced editing options, we can look at UI Explorer.
UI Explorer is used to analyze and edit selectors, particularly when the existing selectors are not stable enough for automation. "Not stable enough" means that the properties of the selectors of the target application keep changing on every execution. UI Explorer has several options to overcome these challenges by fine-tuning the properties of the selector to make it more reliable. Let's explore these options one by one. Click on Open in UI Explorer, as shown in Figure 8.5, to open UI Explorer, as follows:
Let's look at the available options in more detail:
Click on this button if you want to verify the validity of your Selector. The target element must be on the screen before you click this button.
Click on this option to select the target element and generate the selector with all the necessary properties.
This option is used to identify the target element by using the relative elements surrounding the target element.
This button is enabled when the selector is invalid and is used to repair the existing selector by indicating the same target UI element on the screen.
This option is used to highlight the target on the screen for the current selector to ensure it is working.
The Framework settings options are changed based on the application being used. There are three types of frameworks:
This specifies the default settings, which typically work with all types of user interfaces.
This option is used for legacy applications such as mainframes or if the Default option did not work as expected.
This is used for any new applications or if the other two types did not work as expected.
This section gives you the option to explore the elements of the target user interface in a folders and subfolders tree structure. To make the analysis more effective, a Highlight option is provided so that you can examine the UI elements from the tree.
This section gives the option to explore the available properties of the target application.
This section contains the XML fragment that has the tag names and attribute values of the target element. These can be edited to fine-tune the selector.
This section is used to edit the attributes of the selected items in the node.
This section displays the list of selected and unselected attributes of the selector. This gives us the option to analyze the selector by selecting and unselecting attributes.
In this section, we explored the different options that are available within UI Explorer to enhance the selector so that it can handle dynamic target elements. In the next section, we'll compare the different selector types and how they can be applied in different scenarios.
The selector types depend on the type of recorders or activities that are being used inside the UiPath workflows. There are two types:
Let's look at them in more detail.
Partial selectors are generated when you use the Desktop Recorder. It does not contain information about the top-level window. Partial selectors are enclosed inside a container. While we're on the same application screen and interacting with multiple elements, it is a good option to use partial selectors. With partial selectors, the top element (which is grayed out) is inherited from the parent. Let's look at an example where an Attach Window activity is being used in a Notepad application and the selector of the Click activity looks as follows, where the first line can't be edited:
If you want to edit the attributes of the top-level window, you may need to take a different recording approach known as full selectors, which we will look at in the next section.
Full selectors are generated when we are using the Basic Recorder. Full selectors contain all the necessary nodes, including the top-level window. This contains the application node and the final node, which are required to identify the UI element.
Full selectors should be used when you're switching between multiple windows or activities.
For example, when you're switching between multiple applications for data such as Notepad, Word, or Excel, you should use full selectors:
The following table shows the differences between partial selectors and full selectors:
In this section, we looked at the differences between partial selectors and full selectors. In the next section, we will learn about dynamic selectors and see how they can be implemented by looking at a real-life example.
Before we begin discussing dynamic selectors, let's look at static selectors. Static selectors are used to indicate an element in the user interface that does not change its properties or that the element is stable enough. So, we do not need to change the properties in the Selector Editor window, such as the tags and attributes of the selector. A dynamic selector is used to easily identify the target element, which changes its selector properties more frequently at runtime. A dynamic selector is created using a variable or argument instead of the static attributes in the selector tags. Instead of hardcoding the values of the target element, the attribute values can be represented using a variable or an argument to handle the target dynamically. The syntax of the dynamic selector is shown here:
Dynamic selectors can be analyzed and modified inside the Edit Selector window. This window displays all the basic options, such as Edit Attributes and Edit Selector, so that you can dynamically modify the selector. However, for more advanced options, such as Visual Tree, Property Explorer, and Selected Items, you can use UI Explorer to easily identify the dynamic properties.
The following table shows the differences between static selectors and dynamic selectors:
Now, let's look at a real-life example that's available on the PacktPub website about dynamically reading the names of three books using a single selector. To understand this example better, we will analyze the web page, selectors, and their properties using UI Explorer.
The following is a typical example of using dynamic values. The book name keeps changing over a certain period and the UiPath robot should dynamically read the name of the book. In this example, we'll have the robot read the names of the first three books. Let's get started:
If the highlighted idx='1' value needs to be changed for you to select the next book, then we need to parameterize this value.
With that, we have successfully implemented dynamic selectors using a practical example from the PacktPub website to dynamically retrieve the names of books. You can apply the same concepts to different scenarios so that you can work with dynamic target elements.
In this chapter, we looked at selectors, including how to work with tags, attributes, and selector properties. Then, we explored the differences between static, dynamic, partial, and full selectors. After that, we learned about UI Explorer and how to work with the selector properties in challenging situations. Finally, we looked at a practical example of using dynamic selectors to implement the concepts we learned about in this chapter.
In the next chapter, you will be manipulating data using Excel and data tables. Here, you will use selectors to retrieve data from applications that communicate with Excel and data tables.
18.223.33.157