Selenium with Java Tutorial
Last Updated :
30 Sep, 2025
Selenium with Java is a tool for automating web application testing across different browsers and platforms. It helps testers build reliable, maintainable and scalable test automation suites.
Key Features
- Platform-independent and supports cross-browser testing.
- Strong community support with wide adoption in the industry.
- Start with Selenium WebDriver setup and basic navigation.
- Learn to handle dynamic elements, waits and form submissions.
- Use TestNG for structured, real-world test automation frameworks.
How Does Selenium Work
Selenium WebDrive works by acting as a bridge between your test scripts and the web browser, sending commands and receiving responses to automate actions.
- User Code: Automation scripts define actions for web elements.
- Selenium Client Library: Acts as a bridge between user code and WebDriver.
- WebDriver API: Provides standard commands for browser automation.
- Browser Driver: Each browser (ChromeDriver, GeckoDriver, etc.) communicates with WebDriver.
- Browser: Executes the commands (clicks, typing, navigation) sent by WebDriver.
Why Select Selenium with Java
Below, we have mentioned the reasons to choose Selenium with Java for testing the application.
- Versatility: Java is widely used for web automation and has plenty of libraries for Selenium.
- Compatibility and Stability: Works seamlessly with Selenium WebDriver for smooth automation.
- Easy to Learn: Clear syntax makes it perfect for beginners and experienced testers alike.
- Large Community: Large developer and tester community to help with tips, tutorials and troubleshooting.
Step 1. Install JDK
Firstly, configure the Java Development Kit on your system.
Step 2. Install IDE – Eclipse, IntelliJ IDEA or NetBeans.
Now, install an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA or NetBeans.
Step 3. Download Selenium WebDriver
Install the Selenium WebDriver library for Java. And Extract the ZIP and add JARs to your project.
Step 4. Install Browser Driver
We need web browsers such as Chrome, Firefox, Edge or Safari. A ChromeDriver executable file that matches your Chrome version. Extract the zip file and copy the path where the chromedriver.exe file is, it is required in further steps. (e.g. - D:/chromedriver_win32/chromedriver.exe)

Create a Selenium with Java Project in Eclipse
Step 1: Launch Eclipse and select File -> New -> Java Project.

Step 2: Enter a name for your project (e.g., SeleniumJavaTutorial) and click Finish.

Step 3: Right-click on your project in Package Explorer and select Properties.

Step 4: Select Java Build Path from the left panel click on the libraries tab and then select Classpath. Click on Add External JARs and browse to the location where you downloaded the Selenium WebDriver library (e.g., selenium-java-4.1.0.zip).

Step 5: Select all the JAR files inside the zip file and click Open and also all the files inside the lib folder. (D:\selenium-java-4.11.0, D:\selenium-java-4.11.0\lib).

Step 6: Click Apply and Close to save the changes.

Step 7: Create a new package under your project by right-clicking on the src folder and selecting New -> Package.

Step 8: Add the name of your package (e.g., WebDriver)

Step 9: Create a new class under your package (e.g., WebDriver) by right-clicking on the package and selecting New -> Class, then Enter a name for your class and click Finish.

Step 10: After all the steps your file structure looks like this.

Steps for Writing Code
Step 1: Import the required packages at the top of your class:
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
After importing if still getting errors in import just delete the module-info.java file.
Step 2: Create a main class inside the Web class.
Java
public class Web {
public static void main(String[] args) {
}
}
Step 3: Set the system property for ChromeDriver (path to chromedriver executable). (e.g., D:/chromedriver_win32/chromedriver.exe)
Java
System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
Step 4: Create an instance of ChromeDriver.
Java
WebDriver driver = new ChromeDriver();
Step 5: Navigate to the desired website.
driver.get("http://www.geeksforgeeks.org/");
Step 6: Get and print the page title.
Java
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);
Step 7: Wait for a few seconds.
Java
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Step 8: Close the browser.
driver. Quit();
Below is the Java program to implement the above approach:
Java
package WebDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Web {
public static void main(String[] args) {
// Set the system property for ChromeDriver (path to chromedriver executable)
System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
// Create an instance of ChromeDriver (launch the Chrome browser)
WebDriver driver = new ChromeDriver();
try {
// Navigate to the desired website (GeeksforGeeks in this example)
driver.get("http://www.geeksforgeeks.org/");
// Get and print the page title
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);
// Wait for a few seconds (for demonstration purposes only)
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Output:

Handling Alerts, Frames and Windows
1. Alerts
- Alerts are pop-up messages in web applications.
- Selenium provides Alert interface to handle them:
Java
import org.openqa.selenium.Alert;
// Switch to alert
Alert alert = driver.switchTo().alert();
alert.accept(); // Click OK
alert.dismiss(); // Click Cancel
alert.getText(); // Get alert text
2. Frames
- Frames embed one HTML document inside another.
- Use switchTo().frame() to interact with elements inside frames:
Java
driver.switchTo().frame("frameName"); // By name/id
driver.switchTo().frame(0); // By index
driver.switchTo().defaultContent(); // Back to main page
3. Multiple Windows / Tabs
- Selenium can switch between browser windows/tabs using windowHandles:
Java
String parentWindow = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
// Switch back to parent window
driver.switchTo().window(parentWindow);