Wednesday, November 5, 2025

thumbnail

Introduction to WebDriver Interface in Java

🧭 Introduction to WebDriver Interface in Java

1. What is WebDriver?


WebDriver is an interface provided by the Selenium library in Java.

It is used to automate web browsers — allowing testers and developers to simulate user interactions such as clicking buttons, entering text, and navigating between pages.


WebDriver acts as a bridge between your test scripts and the web browser.


2. Package and Definition


The WebDriver interface is part of the org.openqa.selenium package.


package org.openqa.selenium;

public interface WebDriver {

    void get(String url);

    String getCurrentUrl();

    String getTitle();

    void close();

    void quit();

    WebElement findElement(By by);

    List<WebElement> findElements(By by);

    // ... other methods

}



It defines methods that must be implemented by browser-specific classes such as:


ChromeDriver


FirefoxDriver


EdgeDriver


SafariDriver


Each of these classes provides its own implementation of the WebDriver interface to control a specific browser.


3. Commonly Used Methods

Method Description

get(String url) Opens a specified URL in the browser.

getTitle() Returns the title of the current web page.

getCurrentUrl() Returns the current page’s URL.

findElement(By by) Finds a single web element using a locator (e.g., ID, name, XPath).

findElements(By by) Finds multiple elements and returns them as a list.

close() Closes the current browser window.

quit() Closes all browser windows opened by WebDriver.

4. Example Program

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class WebDriverExample {

    public static void main(String[] args) {

        // Set path to the ChromeDriver executable (if needed)

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


        // Create an instance of ChromeDriver (implements WebDriver)

        WebDriver driver = new ChromeDriver();


        // Open a website

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


        // Print the title and URL

        System.out.println("Title: " + driver.getTitle());

        System.out.println("Current URL: " + driver.getCurrentUrl());


        // Close the browser

        driver.quit();

    }

}


5. Key Points


WebDriver is an interface, not a class.


You cannot instantiate it directly; you must use one of its implementations (e.g., new ChromeDriver()).


It supports multiple browsers.


Provides a simple, object-oriented API for browser automation.


6. Hierarchy Overview

WebDriver (interface)

   ├── ChromeDriver

   ├── FirefoxDriver

   ├── EdgeDriver

   ├── SafariDriver

   └── RemoteWebDriver

Learn Selenium with JAVA Training in Hyderabad

Read More

Difference Between Selenium RC, IDE, and WebDriver

How to Install and Configure Eclipse for Selenium Testing

Benefits of Using Selenium for Test Automation

What is Automation Testing? An Introduction with Selenium

Visit Our Quality Thought Institute in Hyderabad

Get Directions























































































Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive