2. First Selenium Webdriver Script: JAVA Code Example (with screenshots)

— 3 min read

Introduction

In this article, we will illustrate how to use the Selenium WebDriver script using Java programming in the Chrome web browser. Furthermore, we will extract the title of the Google website using Selenium WebDriver and compare it with a random string.

Download Selenium Driver

First, we will download the Selenium Driver for the Google Chrome web browser.

Learn Automated Testing: Chromedriverdownload

Here you can download the latest version, which is currently ChromeDriver 81.0.4044.69 – zip file. Download the zip file to your local hard-drive and extract it to a location for future reference.

Note: Check your Google Chrome browser version and download the driver accordingly.

You can check the browser version by clicking on the top right-corner, Help, and About Google Chrome. If your browser version is 80.0.3987.149, then make sure you download the correct driver according to it.

Start Coding in Eclipse IDE

Second, let us open the Eclipse IDE to implement our coding for today’s task. Right-click on the package and create a new class.

Give a suitable name for the class. We will call this class MyClassX for the tutorial

.

Now you have to import the necessary packages to the program. The packages you need to import are:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

After importing the packages, define the SetProperty method. The browser does not have a server to communicate with your Java codes. Therefore, the SetProperty method connects the browser and your codes with the Chrome driver.

Since the ChromeDriver implements the wire protocol, it is fully compatible with any WebDriver client. Simply start up theChromeDriver executable (that works as a server), create a client, and away you go:

System.setProperty("webdriver.chrome.driver","D:\\\\chromedriver\_win32\\\\chromedriver.exe");

Next, you need to import the WebDriver interface. The WebDriver contains all the necessary functions that you need for your project.

WebDriver driver=new ChromeDriver();

Just look at the few functions the Eclipse IDE suggests shown in the below screenshot:

Finally, we will indicate the website for the driver to visit and extract the title - you can give any website you like.

_driver.navigate().to("_**[**_https://www.google.com/_**](https://www.google.com/)");

Extract the Title of a Website

Now you can use the getTitle() method to extract and store it in a variable.

String theTitle = driver.getTitle();

Decide what string you want to compare with the title you are going to extract. Here we will create a variable that will store the expected title.

String expectedTitle = “Google”;

Now we can use if-else condition to compare both the strings.

if(theTitle.equals(expectedTitle))

System.out.println("Page title is as expected ");

else

System.out.println("Page title is as NOT expected ");

Since both the actual title and the expected title are the same, we can expect a positive output.

The complete Eclipse Java Code is as follows:

package myPackage;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
 
public class myClass {
    public static void main(String\[\] args) { 
       System.setProperty("webdriver.chrome.driver", "D:\\\\chromedriver\_win32\\\\chromedriver.exe"); 
       WebDriver driver=new ChromeDriver(); 
       driver.navigate().to("https://www.google.com/"); 
       String theTitle = driver.getTitle();
       String expectedTitle = "Google";
       if(theTitle.equals(expectedTitle))
           System.out.println("Page title is as expected ");
       else
           System.out.println("Page title is as NOT expected ");
 }    
}

Conclusion

You can do much more using the Selenium WebDriver, and extracting the title is one of the basics. We hope this article was helpful and recommend you to explore more methods available in Selenium WebDriver. See you in another article.