Session 19: Using Different Locators

Welcome to Session_19 of QaTestology' "Learn Selenium Easily" series! In this session, you'll learn how to use different locators to identify web elements on a webpage. Mastering locators is crucial for writing effective and reliable test scripts in Selenium.

Why Learn Different Locators?

  • Accuracy: Ensures your test scripts interact with the correct elements.

  • Flexibility: Provides multiple ways to find elements, accommodating changes in the webpage structure.

  • Efficiency: Improves the speed and reliability of your automated tests.

What You'll Learn

  • ID Locator: Locate elements using their ‘id‘ attribute.

  • Name Locator: Locate elements using their ‘name‘ attribute.

  • Class Name Locator: Locate elements using their ‘class‘ attribute.

  • Tag Name Locator: Locate elements by their HTML tag.

  • Link Text and Partial Link Text Locators: Locate links by their text.

  • CSS Selector: Use CSS selectors to locate elements.

  • XPath: Use XPath expressions to locate elements.

Example Code

Here’s an example demonstrating how to use different locators with Selenium WebDriver in Java:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsExample {
public static void main(String[] args) {
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Initialize the ChromeDriver
WebDriver driver = new ChromeDriver();

// Navigate to a website
driver.get("https://www.example.com");

// Locate element by ID
WebElement elementById = driver.findElement(By.id("elementId"));
elementById.click();

// Locate element by Name
WebElement elementByName = driver.findElement(By.name("elementName"));
elementByName.sendKeys("Test");

// Locate element by Class Name
WebElement elementByClassName = driver.findElement(By.className("elementClass"));
elementByClassName.click();

// Locate element by Tag Name
WebElement elementByTagName = driver.findElement(By.tagName("button"));
elementByTagName.click();

// Locate element by Link Text
WebElement elementByLinkText = driver.findElement(By.linkText("Example Link"));
elementByLinkText.click();

// Locate element by Partial Link Text
WebElement elementByPartialLinkText = driver.findElement(By.partialLinkText("Example"));
elementByPartialLinkText.click();

// Locate element by CSS Selector
WebElement elementByCssSelector = driver.findElement(By.cssSelector(".elementClass"));
elementByCssSelector.click();

// Locate element by XPath
WebElement elementByXPath = driver.findElement(By.xpath("//tag[@attribute='value']"));
elementByXPath.click();

// Close the browser
driver.quit();
}
}

Explanation of the Example

  1. Setup WebDriver:

    • Set the system property for ‘ChromeDriver’ to specify the path to the ‘ChromeDriver’ executable.
    • Initialize a new instance of ‘ChromeDriver‘.
  2. Navigate to a Website:

    • Use the ‘get‘ method to navigate to a specified URL.
  3. Locate Elements Using Different Locators:

    • ID Locator: Locate an element by its ‘id‘ attribute using ‘By.id("elementId")‘.
    • Name Locator: Locate an element by its ‘name‘ attribute using ‘By.name("elementName")‘.
    • Class Name Locator: Locate an element by its ‘class‘ attribute using ‘By.className("elementClass")‘.
    • Tag Name Locator: Locate an element by its HTML tag using ‘By.tagName("button")‘.
    • Link Text Locator: Locate a link by its text using ‘By.linkText("Example Link")‘.
    • Partial Link Text Locator: Locate a link by partial text using ‘By.partialLinkText("Example")‘.
    • CSS Selector: Locate an element using a CSS selector with ‘By.cssSelector(".elementClass")‘.
    • XPath: Locate an element using an XPath expression with ‘By.xpath("//tag[@attribute='value']")‘.
  4. Perform Actions:

    • Click or interact with the located elements to perform specific actions.
  5. Close the Browser:

    • The ‘quit‘ method closes the browser and ends the WebDriver session.

Watch the Video Tutorial

For a detailed walkthrough, watch our YouTube video: Session 19: Using Different Locators. This video provides visual examples and explanations to help you understand and apply different locator strategies in Selenium effectively.

By the end of this session, you'll be able to use various locators to identify web elements in your Selenium tests. This skill is crucial for creating robust and maintainable automated tests. Keep practicing and experimenting with different locator strategies to enhance your test automation capabilities.