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
To automate browser interactions using Selenium in Java, you’ll need to follow these steps:

1. Set Up Selenium in Your Project:

  • Download Selenium WebDriver.
  • Add the Selenium WebDriver dependency to your project (for Maven, add it to your pom.xml).
  • Download the browser-specific driver (like ChromeDriver for Chrome).

2. Write a Selenium Script to Open a Browser and Perform Actions:

  • Initialize the WebDriver.
  • Open the browser.
  • Navigate to a URL.
  • Perform actions like clicking, typing, etc.
  • Close the browser.

Step-by-Step Guide

1. Set Up Your Project

Using Maven:

Add the Selenium WebDriver dependency to your ‘pom.xml‘:
				
					<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.6.0</version>
</dependency>

				
			

2. Download the Browser Driver

For Chrome, download ChromeDriver from here.

Make sure to place the driver in a directory included in your system’s PATH or specify its path in your script.

3. Write the Selenium Script

Here’s an example script to open a browser, navigate to a URL, perform some actions, and then close the browser:

				
					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");

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

        // Open a browser and navigate to a URL
        driver.get("http://example.com");

        // Perform actions: Example - locate an element and interact with it
        WebElement exampleElement = driver.findElement(By.id("example-id"));
        exampleElement.click(); // Click the element

        // Find an input element and type some text
        WebElement inputElement = driver.findElement(By.name("example-name"));
        inputElement.sendKeys("Selenium Example");

        // Perform additional actions as needed
        // ...

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

				
			

Detailed Breakdown of the Code

1. Set the Path to the Driver:

				
					System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

				
			

Replace ‘"path/to/chromedriver"‘ with the actual path to your ChromeDriver executable.

2. Initialize the WebDriver:

				
					WebDriver driver = new ChromeDriver();

				
			

This creates a new instance of the ChromeDriver, which opens a new browser window.

3. Open a URL:

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

				
			

This navigates the browser to the specified URL.

4. Locate and Interact with Elements:

				
					WebElement exampleElement = driver.findElement(By.id("example-id"));
exampleElement.click();

				
			

This locates an element by its ID and clicks it. Similarly, other locators like ‘By.name‘, ‘By.className‘, ‘By.tagName‘, ‘By.linkText‘, ‘By.partialLinkText‘, ‘By.cssSelector‘, and ‘By.xpath‘ can be used.

5. Type Text into an Input Field:

				
					WebElement inputElement = driver.findElement(By.name("example-name"));
inputElement.sendKeys("Selenium Example");

				
			

This finds an input element by its name and types the specified text into it.

6. Close the Browser:

				
					driver.quit();

				
			

This closes the browser and ends the WebDriver session.

Advanced Examples

Taking a Screenshot:

				
					import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

// Inside your main method or test method
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("path/to/screenshot.png"));

				
			

Handling Alerts:

				
					// Switch to alert and accept it
driver.switchTo().alert().accept();

				
			

Handling Frames:

				
					// Switch to a frame by index
driver.switchTo().frame(0);

// Switch back to the default content
driver.switchTo().defaultContent();

				
			

Handling Dropdowns:

				
					import org.openqa.selenium.support.ui.Select;

// Locate the dropdown element
WebElement dropdown = driver.findElement(By.id("example-dropdown"));

// Create a Select object
Select select = new Select(dropdown);

// Select an option by visible text
select.selectByVisibleText("Option 1");

// Select an option by value
select.selectByValue("option1");

// Select an option by index
select.selectByIndex(1);

				
			

By mastering these commands and techniques, you can efficiently automate a wide range of web interactions using Selenium in Java.

Scroll to Top