Locators is an order that reveals to Selenium IDE which GUI components ( say Text Box, Buttons, Check Boxes, and so on) its necessities to work on. Locators are one of the fundamental segments of the Selenium foundation, which help Selenium scripts in remarkably distinguishing the Web Elements(such as text box, button, and so forth) present on the site page.
The basic thing, to begin with, is to discover the HTML component in DOM (Document Object Model), for which we need to get the locators. Below are the following steps to locate web element:
Selenium gives the “By” class, which helps in finding components inside the DOM. It offers a few distinct techniques (some of which are in the picture below) like className, CSS selector, id, linkText, name, partial link text, name, and XPath, and so forth, which can recognize the web components dependent on their relating Locators procedures.
We can utilize the class attribute worth from the above DOM to recognize the structure. To distinguish same on the page with the following structure:By.className(“wpcf7-form-control wpcf7-submit”); By confirming that it only selects one or unique element in DOM.
CSS Selector: It is basically used to style Web Pages and is also a good way to locate web elements. Below is the syntax to determine web element with the help of CSS Selector:css=(HTML Page)[Attribute=Value]
If we look at DOM we can see that input tag have following attributes like Type,value,class etc
<input type=”submit” value=”SEND MESSAGE” class=”wpcf7-form-control wpcf7-submit” aria-invalid=”false”>So we can use any of this attribute to locate web element. For example: By.cssSelector(“input[type= ‘submit]”); By confirming that it only selects one or unique element in DOM.
If we look at DOM we can see that input tag have following attributes like name, type, class, value etc
<input type=”text” name=”Name” value=”” size=”40″ class=”wpcf7-form-control wpcf7-text wpcf7-validates-as-required” aria-required=”true” aria-invalid=”false” placeholder=”Your Name:”>So we can use name attribute to locate web element. For example: By.name(“Name “); By confirming that it only selects one or unique element in DOM.
Link and Partial link Text: Text and Partial text of hyperlink can also be used to locate element
If we look at DOM we can see the text of the anchor tag i.e. Platforms.
We can use this text or a partial part of this text to locate elements in selenium. See below code snippet:By.linkText(“Platforms”); By partialLinkText (“Plat”); By confirming that it only selects one or unique element in DOM.
Tag Name: We can likewise utilize a tag to find elements.Tag name include all HTML tag i.e. input, div, a ,button etc .See below Syntax for Tag name:By.tagName(“a”); By confirming that it only selects one or unique element in DOM.
XPATH: Xpath is the language used to inquire about the XML archive. The equivalent can individually distinguish the web component on any page. A syntax that is used to find element using XPath is as follow://tag_name[@attribute_value].
Here tag_name belongs to a particular tag in DOM structure and the attribute is a characteristic of the particular component that can recognize the web component uniquely.The Xpath of the above component is: //input[@placeholder=’Your Name:’] Look at the below code snippet to find element using XPath: By.xpath(“ //input[@placeholder=’Your Name:’] “); By confirming that it only selects one or unique element in DOM.