Methods for Locating Elements
The Selenium WebDriver library provides CSS selectors and XPath statements for selecting HTML elements. See 4. Locating Elements for the list of the library methods and usage examples.
Best Practices for Locating Elements
When locating elements, you are recommended to do the following. Think of it as a checklist.
Understand the following about your application:
the DOM structure
which pages are dynamically and statically loaded
which elements of a page are loaded and visible.
- Use unique IDs for elements and selectors that are as short as possible: Selectors break all the time. Long hierarchical selectors break more easily than shorter ones, and using short selectors will reduce script maintenance over time.
- Click on the user-visible element instead, or send a Return key instead of submitting forms using the method
- Pay attention to element visibility. The specification is complex, and the results are not always what users expect. See the WebDriver specification for more information.