Selenium WebDriver Basics

WebDriver is an interface. Selenium provides different classes like ChromeDriver and FirefoxDriver that implement this interface for each browser. Your code talks to the interface, and the interface talks to the correct browser behind the scenes.

Diagram: WebDriver As A Middleman

     Your Java/Python Code
             |
             v
      WebDriver Interface
       /      |       \
      v       v        v
ChromeDriver FirefoxDriver EdgeDriver
      |       |        |
      v       v        v
   Chrome   Firefox    Edge

This design lets you switch browsers by changing one line of code. The rest of your test logic stays the same.

Creating A WebDriver Object

You create a driver object first, then use it to control the browser.

WebDriver driver = new ChromeDriver();

This single line launches a Chrome browser window and hands you a driver object. Every future command runs through this object.

Common WebDriver Methods

get()

Loads a URL in the browser.

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

getTitle()

Returns the current page title as text.

getCurrentUrl()

Returns the URL currently loaded in the browser.

getPageSource()

Returns the full HTML code of the current page as a string.

close()

Closes the current browser tab only.

quit()

Closes every tab and ends the entire browser session. Most scripts call quit() at the end to free system resources.

Locating One Element

WebDriver finds page elements using the findElement() method paired with a locator strategy.

WebElement searchBox = driver.findElement(By.id("search"));
searchBox.sendKeys("Selenium course");

This code finds the search box by its ID attribute and types text into it.

A Complete Beginner Script

WebDriver driver = new ChromeDriver();
driver.get("https://www.estudy247.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
driver.quit();

This script opens a page, reads its title, prints the title, and closes the browser. Every Selenium project builds on this same pattern.

Leave a Comment

Your email address will not be published. Required fields are marked *