Hi Troy,
I'm going to weigh in on this.
In my opinion, CSS will be the best option in all cases, followed by xPath in a close second.
The thing is, you can't always trust the CSS generated by the browser, and you usually have to do some tweaking in it.
I've compiled some tips I think are useful when choosing your CSS selector.
1 - Remove classes
#MainCopy_ctl06_ucMessageList_rptMessageList_pnlMessage_1 > div:nth-child(1) > div.col-md-8.col-xs-12 > div > div > span
One of the divs has the class ".col-md-8.col-xs-12". People used to web design will recognize this, but what you need to know is that if your screen size changes, this selector will change, and your robot will stop working. So, how can we improve this?
#MainCopy_ctl06_ucMessageList_rptMessageList_pnlMessage_1 > div:nth-child(1) > div > div > div > span
It still works, and it isn't dependant on the screen size.
2- Don't depend on ID's that look 'machine-generated'.
If you try to get the CSS of an element that has an id(id="loginInput"), the selector generated by the browser will be the id with a #(#loginInput).
That's fine for most cases, but sometimes you will find something like this:
id="luid-1f0349e8-7fda-40c9-aedc-b73be4a9bd47"
This wasn't a name chosen by a human programmer, so this ID will likely change in the future (often even after you refresh the page).
Here is a video showing you how to generate a css selector without depending on an element ID.
3- Don't trust selectors that depends on too many elements.
As your selector gets bigger, it has more and more dependencies. That means that even if one of those elements changes, your whole selector stops working.
#MainCopy_ctl06_ucMessageList_rptMessageList_pnlMessage_1 > div:nth-child(1) > div.col-md-8.col-xs-12 > div > div > span
<span class="timeAgoFormat text-muted" data-source="rh_department">Posted yesterday</span>
How can we make this selector smaller?
In this case,
span
has a class called "timeAgoFormat". We can add this tag to its
span.timeAgoFormat
But now, instead of matching only the one span, it matches all of them on the page. So we could use the attribute selector:
span[data-source='rh_department']
You will find more information about CSS selectors here
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
4- Debugging selectors without running your bot every time
To check if your selector works, open the "developer console" in the browser, go into the console, and use this syntax:
document.querySelectorAll("your-selector")
There are other functions with similar capabilities, but querySelectorAll returns an array of all the elements your selector matches (instead of only the first one). This is useful not to see if your selector matches exactly the elements you want to, not more and not less.
Hope that helps!
------------------------------
Lucas Lima Oliveira
------------------------------
Original Message:
Sent: Thu January 14, 2021 03:10 PM
From: Troy DesBarres
Subject: HTML Elements
Hi there
As I continue to create robots focused around websites and web apps I have noticed that one day they work and then about 4 days later the robots stop working. I have come to determine that it is because I am using the css selector type when creating my Set Value to Field Command. I use css because through my playing around that seems to be the only one that I can get working though there are many to choose from.
Is there a document outlining how to use the other selector types effectively I am trying to determine if there is an approach that is less app to change when using the Set Value to Field Command. I am finding that the css selector value changes too often.
Thanks
------------------------------
Troy DesBarres
------------------------------