ID Locators helps locate the GUI elements (Text Box, Buttons, Check Boxes etc.) through which multiple user actions can be performed. There are one of the important parameters for scripting. Selenium provides some set of Locators to locate a GUI element. The choice of locator largely depends on the Application Under Test.
Elements can be located in WebDriver by using the method “findElement(By.locator())“.
In case of locating multiple items, we used methods findElements(By.locator())
ID Locator the most common way of locating elements considering as they are unique to each element in the DOM (Document Object Model) structure. As per W3C, ID’s would be unique to elements and hence they are considered as the fastest and safest method to locate element. If there is no DOM element available with the given ID, then NoSuchElementException is raised in that case.
Syntax: WebElement element = driver.findElement(By.id(<ID>));
Let’s take an example by locating “Forms” in “https://demoapp1.tequality.tech/” page.
Step1: Open “https://demoapp1.tequality.tech/“ in browser
Step2: Hover your mouse to Forms present in side-menu
Step3: Right click on the mouse and do inspect element
WebElement element = driver.findElement(By.id("side-menu-form"));