Selenium WebDriver Interface

Introduction to WebDriver Interface

The WebDriver interface is the main interface in Selenium. It provides methods for interacting with web pages, such as opening a URL, finding elements on a page, navigating back and forth, and switching between browser windows.

Note: The first thing in our test script is to create an object of the WebDriver interface to open a browser window and work with the web application.

Syntax to use WebDriver interface

To use the WebDriver interface in our Selenium tests, we need to create an object of the WebDriver interface. Here is the syntax to create an object of the WebDriver interface:

  • Create an object of the WebDriver interface as per the browser you want to automate testing. For example, to automate testing in Google Chrome, create an object of the ChromeDriver class as below:
    WebDriver driver = new ChromeDriver();
  • Then use the methods of the WebDriver interface to work with the web application. For example, to open a URL in the browser, use the get() method as below:
    driver.get("https://www.nicetesters.com");

WebDriver Implementation Classes

The WebDriver interface has several implementation classes, each corresponding to a different browser. Based on the browser we want to write selenium script, we can create an object of the corresponding implementation class. Some of the WebDriver implementation classes are:

  • ChromeDriver: For Google Chrome
  • FirefoxDriver: For Mozilla Firefox
  • InternetExplorerDriver: For Internet Explorer
  • EdgeDriver: For Microsoft Edge
  • SafariDriver: For Safari

Here is an example of using the WebDriver interface to open a website in different browsers:

public class WebDriverExample {
    public static void main(String[] args) {
        // Set the path to the WebDriver executables
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
        System.setProperty("webdriver.edge.driver", "path/to/edgedriver");
                
        // Create instances of WebDriver for different browsers
        WebDriver chromeDriver = new ChromeDriver();
        WebDriver firefoxDriver = new FirefoxDriver();
        WebDriver edgeDriver = new EdgeDriver();
    }
}
Note: The System.setProperty() method is used to set the path to the browser driver. We need to download the browser driver and set the path before creating an object of the WebDriver.

WebDriver Methods Details

We will look into some of the WebDriver methods with examples:

  1. get(String url): This method is used to open a new browser window and navigate to the specified URL. We need to pass the URL of the website as a parameter to this method. For example:
    driver.get("https://www.nicetesters.com");
  2. getTitle(): This method is used to return the title of the current page. For example:
    String title = driver.getTitle();
  3. getCurrentUrl(): This method is used to return the URL of the current page. For example:
    String url = driver.getCurrentUrl();
  4. findElement(By by): This method is used to find the first WebElement using the given method. It takes a By Class object as a parameter to find the element on page and return the WebElement object. For example:
    WebElement element = driver.findElement(By.id("elementId"));
  5. findElements(By by): This method is used to find all WebElements for the provided locator. It takes a By Class object as a parameter to find the elements on page and return a List<WebElement> object. . For example:
    List<WebElement> elements = driver.findElements(By.tagName("a"));
  6. close(): This method is used to close the current browser window. For example:
    driver.close();
  7. quit(): This method is used to close all browser windows and end the WebDriver session. For example:
    driver.quit();
  8. navigate(): This method is used to return an object of the Navigation interface, which allows you to navigate back and forward in the browser's history. For example:
    driver.navigate().back();
  9. manage(): This method is used to return an object of the Options interface, which allows you to manage browser options such as cookies, timeouts, and window size. For example:
    driver.manage().window().maximize();

WebDriver methods code example

Here is an example of using the WebDriver interface to open a website in different browsers:

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

        // Create instances of WebDriver for chrome browsers
        WebDriver chromeDriver = new ChromeDriver();

        // Open a website in Chrome
        chromeDriver.get("https://www.nicetesters.com");
        System.out.println("Chrome - Title: " + chromeDriver.getTitle());
        chromeDriver.quit();

        // Create instances of WebDriver for Firefox browsers
        WebDriver firefoxDriver = new FirefoxDriver();

        // Open a website in Firefox
        firefoxDriver.get("https://www.nicetesters.com");
        System.out.println("Firefox - Title: " + firefoxDriver.getTitle());
        firefoxDriver.quit();

        // Create instances of WebDriver for Edge browsers
        WebDriver edgeDriver = new EdgeDriver();

        // Open a website in Edge
        edgeDriver.get("https://www.nicetesters.com");
        System.out.println("Edge - Title: " + edgeDriver.getTitle());
        edgeDriver.quit();
    }
}

In this example, we created instances of the ChromeDriver, FirefoxDriver, and EdgeDriver classes to open a website in Google Chrome, Mozilla Firefox, and Microsoft Edge browsers, respectively. We then printed the title of the website and close the browser window.

WebDriver Methods Reference

Method Description
get(String url) Opens a new browser window and navigates to the specified URL.
getTitle() Returns the title of the current page.
getCurrentUrl() Returns the URL of the current page.
findElement(By by) Finds the first WebElement using the given method.
findElements(By by) Finds all WebElements using the given method.
close() Closes the current browser window.
quit() Closes all browser windows and ends the WebDriver session.
navigate() Returns an object of the Navigation interface, which allows you to navigate back and forward in the browser's history.
manage() Returns an object of the Options interface, which allows you to manage browser options such as cookies, timeouts, and window size.
Next Steps: We have seen findElement() and findElements() methods in the above example. But we pass the By class object as a parameter to these methods. Let's see how to use the By class in the next tutorial.