Selenium WebDriver API Commands And Operations
Fetching a page
You can navigate from one page to another page by just calling a get method
Dependent on several factors, including the OS/Browser combination, WebDriver may or may not wait for the page to load. In some circumstances, WebDriver may return control before the page has finished, or even started, loading. To ensure robustness, you need to wait for the element(s) to exist in the page using explicit or implicit wait.
Locating Web Elements or UI Elements
Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings expose a “Find Element” and “Find Elements” method. The first returns a WebElement object otherwise it throws an exception. The latter returns a list of WebElements, it can return an empty list if no DOM elements match the query.
The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.
Example of how to find an element that looks like this:
- <div id="coolestWidgetEvah">...</div>
- webElement Element=driver.findElement(By.id("coolestWidgetEvah"));
This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.