If you have ever struggled to create Dynamic Selectors in Microsoft Power Automate for desktop, fret not! This article will walk you through the process.
Why use dynamic selectors in Microsoft Power Automate for desktop
One of the challenges of automation is making sure that the bots are robust, and can still work, even if conditions are not exactly the same. Making your RPA script dynamic helps to prevent your bot from breaking when there are changes in the UI.
- Example Situation
- Read data from Excel
- Launch Chrome, navigate to the website, and add UI elements
- Creating a dynamic selector
- Create your own custom selectors
- Complete your Microsoft Power Automate for desktop flow
If you prefer to follow video instructions, you can have a look at the video below. If you prefer to read, just scroll down further for the article.
Example situation requiring dynamic selectors when automating with Microsoft Power Automate for desktop
This article will be focused on the RPA challenge (https://www.rpachallenge.com/). The goal of the challenge is to create a workflow that will input data from a spreadsheet into form fields, which change position after every submission.
For this article, we’ll use the RPA challenge to illustrate the steps of creating dynamic selectors.
The task at hand will be to write a loop to fill in the values in every row from the excel given, regardless of the position of the fields on the screen.
Before you start creating your flow, first download the Excel file given on the website.
Here’s what the data looks like:
Read data from Excel
Now, on your Microsoft Power Automate for desktop, use the “Launch Excel” action, select “and open the following document” from the dropdown menu, and set the Document Path.
This will be followed by the “Get first free column/row from Excel worksheet” action.
Next, we will add the “Read from Excel worksheet” action into our flow. Remember that the End column and End row should be set as %FirstFreeColumn-1% and %FirstFreeRow-1%, respectively.
Launch Chrome, navigate to the website, and add UI elements
The next action we will be adding to our workflow is the “Launch new Chrome” action. Under “Initial URL”, copy paste the link for the RPA challenge, and set the “Window state” to “maximised”.
We will now begin creating our loop. Add the “For each” action to your workspace and set the “Value to iterate” as %ExcelData% (you can choose it by clicking on the {x} button).
Add the “Populate text field on web page” action within the “For each” loop. Click “Add UI element” under “UI element”.
Navigate to the browser and select the UI elements by holding down the Control key and left-clicking the element you want to select, then click Done, and click Save.
Doing this is not enough. It will not work because every time we click submit, the field will have a different ID. So, we need to make this dynamic.
If you’re curious, this is what happens when you try to recapture a new selector for the same UI element after clicking submit. Note the different IDs.
Creating a dynamic selector in Microsoft Power Automate for desktop
At the browser (on the RPA Challenge page), right-click on the field you’re working on – in this case the “First Name” – then click “inspect”. Under the elements tab, look for a common name that does not change after you click submit repeatedly. In this case, the “labelFirstName” remains the same.
Copy the text, including the part before “labelFirstName”. See our example below, enclosed in the red box.
Toggle back to Microsoft Power Automate for desktop. In the UI Element pane, click the 3 dots next to the UI Element you’ve selected earlier, and click “Edit Selectors”.
A dialogue box showing “selectors of the UI element ” will appear. Click the 3 dots beside one of the selector for the UI element:
The Selector builder dialogue box will appear. Unselect the “Custom” button at the bottom.
Then, remove the id and paste the copied text block you copied earlier. This will allow your selector to work even after clicking the “Submit” button—you have a dynamic selector!
In the UI elements pane, rename the UI element to “FirstName”, so that you can refer to it easily later on.
Next, we will need to add another element for the “submit” button on the webpage. Follow the same steps as above and rename it as “Submit”.
Add the “Press button on web page” action and set the UI element as “Submit”.
However, if we stop here, the flow will not work the next time it’s run. Click on the 3 dots icon icon next to the “Submit” element. Click on “Edit selectors”.
Once the Selectors of UI element “Submit” dialogue box opens up, click “Edit selector”.
Uncheck all the different attributes, enabling only the Class attribute.
Double-click on the “Populate text field on webpage” action we added earlier, and enter %CurrentItem[‘First Name’]% in the “Text:” field, and click Save. This takes the current value from the column “First Name” in the Excel file and populates it into the First Name field on the RPA challenge web page.
After running the flow, you will see that the flow is working and successfully filling in the “First Name” field from the data in the given Excel file!
Create your own custom selectors
Protip: You can also create your own custom selectors using the Type attribute.
We will illustrate this with the example of the Submit button.
Start by inspecting the elements of the Submit button as we did earlier, then copy the type=”submit” (see picture below) from the elements tab.
Next, you’ll need to and paste what you copied into the “Selector builder”.
To do this, go to the UI Elements pane, click on the 3 dots beside the Submit UI element, and select “edit selectors”. In the Selectors of UI element “Submit” dialogue box, click “New”.
Then, replace the class with type=”submit”, which you copied earlier.
Now you have two selectors. So if the first selector does not work, the loop will try again using the second selector.
Now, repeat all earlier steps with the remaining fields – Role in Company, Company name, Last Name, Phone Number, Email.
Complete your Microsoft Power Automate for desktop flow
Now that your UI elements have been set up correctly with dynamic selectors, it’s time to complete the flow that will allow you to fill up all the fields in the RPA challenge correctly.
First, to prevent multiple instances of Excel to be open after running the flow multiple times, you can insert the close Excel action after the loop.
You can also add the “Terminate process” action at the beginning, and select “EXCEL” as the process name.
Next, work on getting your flow to fill up all the fields in the RPA challenge website.
Duplicate the “populate text field on web page” action you’ve set up earlier, and change the details (such as the “Text” field and the UI element for each action) to correspond to each of the fields.
To duplicate an action, you can select it and press Ctrl + C to copy, then Ctrl + V to paste.
Running the flow will show that you have managed to fill in all fields accurately from the Excel data given, regardless of the position of the fields! You have successfully created your own dynamic and customised selectors!
Start using dynamic selectors in Microsoft Power Automate for desktop
The example we show here is just the tip of the iceberg. There are plenty of ways to use dynamic selectors in Microsoft Power Automate for desktop to make your solution more robust. What are some of the ways you are thinking of using dynamic selectors in Microsoft Power Automate for desktop? Share it with us in the comments!