Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

In Selenium for Java, the By class is used to locate elements on a web page. One common way to find elements is by their HTML id attribute. Here’s a detailed description of how to use locators by ID in Selenium Java:

Importing Selenium Classes

First, ensure you have imported the necessary Selenium classes:

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

				
			

Setting Up WebDriver

Set up the WebDriver to use your preferred browser (in this example, Chrome):

				
					System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();

				
			

Navigating to a Web Page

Navigate to the desired web page:

				
					driver.get("https://example.com");

				
			

Locating an Element by ID

To locate an element by its ‘id‘ attribute, you use the ‘By.id‘ method. Here’s how:

				
					WebElement element = driver.findElement(By.id("elementId"));

				
			

Where ‘"elementId"‘ is the value of the ‘id‘ attribute of the HTML element you want to locate.

Example Usage

Here’s a complete example that demonstrates how to locate an element by ID and interact with it. Assume there’s an input field with the ID ‘username‘:

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

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

        // Create a new instance of the Chrome driver
        WebDriver driver = new ChromeDriver();

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

            // Locate the element by ID
            WebElement usernameField = driver.findElement(By.id("username"));

            // Interact with the element
            usernameField.sendKeys("myUsername");

            // Perform other actions as needed

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

				
			

Explanation

  1. Setting up WebDriver: The ‘System.setProperty‘ line sets the path to the WebDriver executable. Replace ‘"path/to/chromedriver"‘ with the actual path on your system.
  2. Creating a WebDriver instance: A new instance of the Chrome driver is created.
  3. Navigating to a webpage: The ‘driver.get‘ method navigates to the specified URL.
  4. Locating an element by ID: The ‘driver.findElement(By.id("username"))‘ method locates the input field with the ID ‘username‘.
  5. Interacting with the element: The ‘sendKeys‘ method is used to enter text into the input field.
  6. Closing the browser: The ‘driver.quit‘ method closes the browser after the actions are performed.
Scroll to Top