Selenium Locators

Click to rate this post!
[Total: 0 Average: 0]

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
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button