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

Selenium is a powerful tool for automating web browsers. When using Selenium with Java, one of the most important tasks is finding and interacting with elements on a web page. Here’s an in-depth look at how to find elements in Selenium with Java, including examples.

Finding Elements

a. By ID

The ‘By.id‘ method is used to find an element by its unique ID attribute.

				
					WebDriver driver = new ChromeDriver();
driver.get("http://example.com");

// Find element by ID
WebElement element = driver.findElement(By.id("elementID"));
element.click();

				
			

b. By Name

The ‘By.name‘ method is used to find an element by its name attribute.

				
					WebElement element = driver.findElement(By.name("elementName"));
element.sendKeys("Some text");

				
			

c. By Class Name

The ‘By.className‘ method is used to find an element by its class attribute.

				
					WebElement element = driver.findElement(By.className("className"));
element.click();

				
			

d. By Tag Name

The ‘By.tagName‘ method is used to find an element by its tag name.

				
					WebElement element = driver.findElement(By.tagName("input"));
element.sendKeys("Some text");

				
			

e. By Link Text

The ‘By.linkText‘ method is used to find a link (anchor tag) by its text.

				
					WebElement element = driver.findElement(By.linkText("Click here"));
element.click();

				
			

f. By Partial Link Text

The ‘By.partialLinkText‘ method is used to find a link by a part of its text.

				
					WebElement element = driver.findElement(By.partialLinkText("Click"));
element.click();

				
			

g. By CSS Selector

The ‘By.cssSelector‘ method is used to find an element by a CSS selector.

				
					WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
element.sendKeys("Some text");

				
			

h. By XPath

The ‘By.xpath‘ method is used to find an element by an XPath expression.

				
					WebElement element = driver.findElement(By.xpath("//input[@id='elementID']"));
element.sendKeys("Some text");

				
			

Finding Multiple Elements

You can also find multiple elements using the ‘findElements‘ method, which returns a list of WebElement objects.

				
					List<WebElement> elements = driver.findElements(By.className("className"));
for (WebElement element : elements) {
    System.out.println(element.getText());
}

				
			

Interacting with Elements

a. Clicking

Click on a button or link.

				
					WebElement button = driver.findElement(By.id("buttonID"));
button.click();

				
			

b. Typing

Type text into an input field.

				
					WebElement input = driver.findElement(By.name("inputName"));
input.sendKeys("Some text");

				
			

c. Clearing Text

Clear the text from an input field.

				
					WebElement input = driver.findElement(By.name("inputName"));
input.clear();

				
			

d. Submitting a Form

Submit a form.

				
					WebElement form = driver.findElement(By.id("formID"));
form.submit();

				
			

Advanced Usage

a. Using WebDriverWait

Sometimes elements are not immediately available. You can use’ WebDriverWait‘ to wait for elements to become available.

				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));
element.click();

				
			

b. Chaining Methods

You can chain methods to find nested elements.

				
					WebElement form = driver.findElement(By.id("formID"));
WebElement input = form.findElement(By.name("inputName"));
input.sendKeys("Some text");

				
			

Example: Complete Code

Here’s a complete example that demonstrates various ways to find and interact with elements.

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

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

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

        // Open a webpage
        driver.get("http://example.com");

        // Find an element by ID
        WebElement elementById = driver.findElement(By.id("elementID"));
        elementById.click();

        // Find an element by Name
        WebElement elementByName = driver.findElement(By.name("elementName"));
        elementByName.sendKeys("Some text");

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

        // Find an element by Tag Name
        WebElement elementByTagName = driver.findElement(By.tagName("input"));
        elementByTagName.sendKeys("Some text");

        // Find an element by Link Text
        WebElement elementByLinkText = driver.findElement(By.linkText("Click here"));
        elementByLinkText.click();

        // Find an element by Partial Link Text
        WebElement elementByPartialLinkText = driver.findElement(By.partialLinkText("Click"));
        elementByPartialLinkText.click();

        // Find an element by CSS Selector
        WebElement elementByCssSelector = driver.findElement(By.cssSelector("input[type='text']"));
        elementByCssSelector.sendKeys("Some text");

        // Find an element by XPath
        WebElement elementByXPath = driver.findElement(By.xpath("//input[@id='elementID']"));
        elementByXPath.sendKeys("Some text");

        // Find multiple elements by Class Name
        List<WebElement> elements = driver.findElements(By.className("className"));
        for (WebElement element : elements) {
            System.out.println(element.getText());
        }

        // Using WebDriverWait
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement elementWithWait = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));
        elementWithWait.click();

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

				
			

This example covers various methods to find and interact with elements in Selenium using Java. Each method is demonstrated with simple code snippets, and a complete example ties them together.

Scroll to Top