findElement method in Selenium WebDriver

To find any web element in any webpage this findElement method is used by Selenium WebDriver. This is an abstract method and defined under the SearchContext interface. as mentioned in the source code. But this SeachContext interface is also implemented by the SeleniumWebDriver interface as mentioned in here. As mentioned in the source code comment section:-

findElement method in Selenium

Here the return type of findElement method is WebElement instance. And it takes the argument of By type, which is a locating mechanism in SeleniumWebDriver. If multiple webElement is present with the same attribute value, then this method will always pick he first matching context. And also it will returned NoSuchElementException if that particular element is not present in the corresponding Web Page.

The sample code block to understand the findelement method:-

WebDriver driver;
WebElement searchBox = driver.findElement(By.name("q"));

In the preceding code block the findElement method can be accessed from the instance of the WebDriver class as WebDriver interface implements the Searchcontext interface where it is defined. And this method actually implemented in the RemoteWebDriver.class.