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 element

findElement(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();
}
}

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.