Check if Selector Is Correct and Unique
Last updated
Last updated
Here is how you can check whether a selector is correct and unique before you run your TaskBot.
Click on the Console tab inside your Inspect mode.
Now enter the following command directly on the console:
document.querySelectorAll("PASTE YOUR SELECTOR HERE")
In the example of Wikipedia's search button, it can be, for example, this selector (but you can use any other from the list you saw in the previous chapter One Element Can Have Many Selector Expressions):
document.querySelectorAll("button[class*='button'][type='submit']")
This is what it looks like in action:
Once you hit Enter, you will see that there is an element shown:
Once you expand the shown element and hover over it, you will see the button highlighted on the left side that is identified by that selector.
If your selector is not unique enough, the result will show multiple selectors. This means you need to get back to work and build a more unique selector. Exception: Unless you are building a selector for a list (such as list of profiles, list of pages, etc.) - in this case keep reading this guide and be sure to pay attention in the sectionLists: Incremental CSS Selectors.
If the result is not showing anything, this means your selector is not working out or you have a typo inside it (for example, you used double quotes instead of single quotes). You need to try again, adjust it or build another one.
When you build your own selectors, always first check them via document.querySelectorAll directly on the website. This way you can avoid running your TaskBot every time you want to check validity of your custom built selector. It will save you a lot of time.
Note that you cannot check correctness of text= (see Use Element's Text) with document.querySelectorAll because this method is unique to ZeroWork.