Try to be as specific in your locators as possible: IDs, and name elements when you can, and if you have to use XPaths, construct them so that they only return the one (or more) elements about which you actually care. (StanVogel)
Make sure when using XPath that the attributes being used do not change (such as an id that changes from one visit on a web page to another). (LeslieReis)
Construct xpath locators with %s and use String.format to insert variables values into the same xpath. For instance if you need to select an option from a dropdown menu, you might use something like ‘xpath = “//input[@id=dropdown]/option[text()=%s]”;’ and then use ‘String.format(xpath, “Desired Option”);’ (KyleRoth)
Use contains() in an xpath to grab part of an ID (accounts for IDs which are not consistent when the application loads) or avoid issues with white space in text. For instance, a text field with an ID of “extAdminPanel_txtSearchField” could have a locator of “//input[contains(@id, ‘AdminPanel’) and contains(@id, ‘SearchField’)]” (KyleRoth)
Sikuli
Use Sikuli locators when needed, but always opt for traditional xpath locators first. For instance a dropdown might need a Sikuli click to open, but then can take an xpath click to choose the option you’re looking for. (KyleRoth)