Selenium Java Tutorial
Locators In Selenium
What Are Locators ?
Locators are used to identify the elements ( say Text Box, Buttons, Check Boxes etc) on web page and to perform the actions. Identification of correct GUI elements is a prerequisite for creating an automation script.
What Are The Different Types Of Locators?
id
name
className
linkText
partialLinkText
tagName
xpath
cssSelector
How To Locate Elements Using Locators?
To locate an element of a web page, first we have to access the DOM of that page.
For accessing the DOM, right-click anywhere within the page and select the inspect option.
A Developer Tools window will be opened with the default tab Elements.
In DOM, hovering the mouse over the HTML tags will highlight it's associated web element on the web page.
To inspect a web element, first enable the mouse icon in DOM and then select a web element on the web page. It will automatically highlight the associated HTML element in the DOM.
Selenium provides 'By' class for locating web elements.
Following are the syntax for different-different locators :
Id ---
By.id ("Value of id");
Name ---
By.name("Value of 'name' attribute");
Class Name ---
By.className("Name of the class");
Link Text ---
By.linkText("Text between anchor <a>text</a> tag");
Partial Link Text ---
By.partialLinkText("Subtext of text between anchor tag");
Tag Name ---
By.tagName("Name of the tag");
CSS Selector ---
By.cssSelector("tag_name[attribute_name='value']");
Xpath ---
By.xpath("//tag_name[@attribute_name='value']");