Using the right locators is one of the most important parts of the UI automation process. A weak locator will almost surely break at one point or another. The locator strategies are very well described on the Selenium.dev website.
We have added some locators strategies of our own. Some of them during time have also been introduced to Selenium itself, so it seems like these additional locator strategies have proven themselves to be useful.
Note! These locators are not supported in the PageFactory Mechanism.
| Locator | Description | Use Case |
|---|---|---|
| AnyBy | Use this locator when looking for an element which has at least one of a few locaors specified | When a version is upgraded and there is still a need to keep the code backward compatible, When it is difficult to pinpoint one specific locator for the element |
| AllBy | Use this locator when looking for an element which match all of the locaors specified | When it is difficult to pinpoint a specific locator for the element |
| NotBy | Use this locator when looking for an element which does not match the locaor specified | When it is difficult to pinpoint a specific locator for the element |
| TDBy | Use this locator when looking for a table element | When looking for an entire table, or a specific cell within a tanle |
| InputBy | Use this locator when looking for an input element | When looking specifically for an input |
| ElementBy | Use this locator when looking for a specific known element |
Next, we will show examples of using each locator strategy:
| Locator | Example |
|---|---|
| AnyBy | driver.findElement(AnyBy.any(By.id("will-not-find"), By.cssSelector("#will-find")); |
| AllBy | driver.findElement(AllBy.all(By.tagName("HaveToBeFound"), By.cssSelector("#will-find")); |
| NotBy | driver.findElement(NotBy.not(By.className("do-not-want-this-class")); |
| TDBy | driver.findElement(TdBy.cellLocation(1,0)); |
| InputBy | driver.findElement(InputBy.label("email")); |
| ElementBy | driver.findElement(ElementBy.partialText("elemet-text")); |