How do you locate elements in Selenium using Java?

    IHUB Talent: Best Selenium with Java Training in Hyderabad with Live Internship

Looking to build a strong career in automation testing? IHUB Talent offers the best Selenium with Java training in Hyderabad, designed to turn you into a job-ready automation tester with practical, hands-on skills and real-world experience through a live internship program.

At IHUB Talent, the training is led by industry experts who bring in real-time scenarios and case studies to ensure you're learning what truly matters in today’s software testing landscape. The course covers Selenium WebDriver, Java programming basics to advanced concepts, TestNG, Maven, Jenkins, Git, and frameworks like POM and BDD (Cucumber) — everything you need to become a full-stack automation tester.

What makes IHUB stand out is the live internship program where students work on real-time projects with actual companies. This practical exposure gives you the confidence to apply your skills in a real environment and helps you build a professional portfolio that impresses recruiters.

Whether you're a fresher, manual tester, or a working professional looking to upskill, IHUB Talent’s job-oriented training with 1:1 mentorship, resume building, and interview prep is the right choice.

The Actions class in Selenium is used to build and perform complex user interactions like mouse movements, keyboard events, drag-and-drop, and other advanced gestures that go beyond simple clicks or typing.

In Selenium with Java, you can locate elements on a webpage using different locators provided by the By class. Locating elements is the first step before performing actions like click, type, or get text.


🔑 Ways to Locate Elements in Java (Using By Class)

1. By ID (most reliable if unique)

WebElement username = driver.findElement(By.id("username"));
username.sendKeys("myUser");

2. By Name

WebElement password = driver.findElement(By.name("password"));
password.sendKeys("myPass");

3. By Class Name

WebElement loginBtn = driver.findElement(By.className("login-btn"));
loginBtn.click();

4. By Tag Name

WebElement firstInput = driver.findElement(By.tagName("input"));

5. By Link Text (for <a> elements)

WebElement signUpLink = driver.findElement(By.linkText("Sign Up"));
signUpLink.click();

6. By Partial Link Text

WebElement forgotPwd = driver.findElement(By.partialLinkText("Forgot"));
forgotPwd.click();

7. By CSS Selector

WebElement email = driver.findElement(By.cssSelector("input[type='email']"));
email.sendKeys("test@example.com");

8. By XPath

WebElement loginBtn = driver.findElement(By.xpath("//button[@id='login']"));
loginBtn.click();

✅ Example Program

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

public class SeleniumLocators {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        // Locate elements using different locators
        WebElement username = driver.findElement(By.id("username"));
        WebElement password = driver.findElement(By.name("password"));
        WebElement loginButton = driver.findElement(By.xpath("//button[@id='login']"));

        // Actions
        username.sendKeys("myUser");
        password.sendKeys("myPass");
        loginButton.click();

        driver.quit();
    }
}

Best Practices

  • Prefer id or name when unique.

  • Use CSS selectors or XPath for complex locators.

  • Avoid className if the class has multiple values.

  • Always wait for elements using implicit/explicit waits before interacting.


👉 Do you want me to also show you how to use findElements (plural) to locate multiple elements like lists or tables in Java?

Read More

Comments

Popular posts from this blog

What is Selenium and how is it used with Python?

What is an implicit wait?

How to set up Selenium WebDriver with Java?