Limited Period Offer - Upto 50% OFF | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Selenium Locators

The Selenium Locators are commands, that tells IDE which selenium IDE elements it needs to operate on. The identification of the correct GUI element is a prerequisite to creating the automation script. using locators, we can match the element pages and interact with them.

Selenium Locators

ID element():

Each element has a unique ID. We can easily find the elements using ID locator. According to W3c, ID's are unique on the page and makes it more reliable. And this is the fastest and safest elementor.

id= id of the elementfindElement(By.id("xxx"))
Name locator():

It is used to identify the elements on the web page. Moreover, locating elements using Name locator is the same as locating using Name locator. But these are not unique on the page. And if there are multiple elements with the same name locator, then the first element on the page is selected. And the test fails, if another element with the same name locator is present on the web page.

name = name of the element

 syn :

findElement(By.name("name"))
Class name locator():

This gives the element that matches the value specified with the class  attribute "class"

 syn :

findElement(By.className("class name"))
Tag name locator():

This is used to find elements matching with a specific tag name. Moreover, it is helpful when we extract the content within a tag.

 syn :

findElement(By.tagName(" Tag Name"))
Link Text locator():

This locator works on hyperlinks. And if there are multiple elements with the same link text then the first one will be selected.

 syn :

findElement(By.linkText("LinkText"))
 Partial Link text locators():

There are some situations, where we need a link to the certain portion of text. In such cases,  we use partial link text locators.

 syn :

findElement(By.partialLinkText("partialLinkText"))
CSS selector Locator():

This the best way to locate the elements on the web page. Many testers believe,  CSS makes the test execution faster. Moreover, testers added, this selector makes the test execution faster. And they added, it is the best way to locate the elements on the page. Selenium supports the following CSS selectors.

Tag and ID :findElement(By.cssSelector(tag#id))Tag and Class:findElement(By.cssSelector(tag.class))Tag and Attribute:findElement(By.cssSelector(tag[attribute=value]))Tag, Class, and Attribute:findElement(By.cssSelector(tag.class[attribute=value]))
Xpath locator:

Xpath locator can easily navigate the XML documents. Its purpose is to select individual elements, attributes, and some other parts for XML document for specific processing. Moreover, this XPath produces reliable locators. But when compared to CSS selector, this has slow performance.

[ Related Article - What is Xpath in Selenium? ]

syn:

findElement(By.xpath("XPath"))

Testing a page :

To start testing the webpage, we need to open the webpage ad provide a URL to navigate to the page. Run the felling code in the Eclipse IDE to navigate to the Facebook login page.

package seleniumWebDriver;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class WebDriverClass{public static void main(String[] args){System.setProperty("webdriver.gecko.driver", "files/geckodriver.exe");WebDriver driver = new FirefoxDriver();driver.get("https://www.facebook.com/onlineitguruatonline");driver.getTitle();driver.quit();}}