Selenium Program to Login to a Specific Web Page

Selenium RC (Selenium Remote Control) is one of the Selenium Components which was developed by Paul Hammant. It is an automation testing tool for web applications. To create test scripts, testers are allowed to use many programming languages such as Java, Python, Ruby, C#, JavaScript, Perl, and PHP. Here we are using Java for writing test scripts.

Table of Content

  • Prerequisites for Login Automation using Selenium Webdriver
  • Steps for Login Automation using Selenium WebDriver
  • Example of Login to a Specific Web Page
  • Conclusion
  • Frequently Asked Questions on Selenium Program to Login to a Specific Web Page

Prerequisites

Steps for Login Automation using Selenium WebDriver

  1. Create a WebDriver Instance
  2. Configure Browser
  3. Navigate to the Web Page
  4. Locate the Web Element
  5. Perform Action
  6. Verify Action

Now, let’s go through each step in detail. These steps are as depicted below with the help of visual aids for better understanding. 

Step 1: To Create a New Project, Click on File –> New –> Java Project.

Create a New Project to login with selenium

After creating a new project, give the project name “Login_Test” and click on Finish. 

Step 2: Create a new Java class 

For creating a new Java class, Right-click on “Test”, Click on new and select Class. Given the class name “Login_test” and click Finish.

Create a new java class 

Now we have to import Selenium Web Driver and Chrome Driver.

Step 3: Download the latest Chrome Driver and Selenium Web Driver for Java.

After downloading, unzip the downloaded files. Right-click on “Testing”, Click on Build Path, and Select Configure Build Path.

Configure Build Path

Step 4: Click on Classpath

4.1: Initially alongside select Add External JARs.

Click on Classpath

4.2: Now go to the Selenium driver downloaded file, select both these jar files, and click on ‘Open’.

Jar files opening

4.3: Go to the same folder, open the “libs” folder, select all the jar files, and click on ‘Open’.

Select all the jar files

After adding all these required Jar files, below is the given code to test the login page.

Example of Login to a Specific Web Page

Java
// Java Program to Login to a specific Webpage
// Using Selenium WebDriver and ChromeDriver

// Importing package module to code fragment
package login_test;

// Importing required classes
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

// Main class
public class Test {

    // Main driver method
    public static void main(String[] args)
    {

        // Path of chrome driver
        // that will be local directory path passed
        System.setProperty(
            "webdriver.chrome.driver",
            "C:\\Users\\Admin\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        // URL of the login website that is tested
        driver.get("https://auth.w3wiki.net/");

        // Maximize window size of browser
        driver.manage().window().maximize();

        // Enter your login email id
        driver.findElement(By.id("luser"))
            .sendKeys("xyz@gmail.com");

        // Enter your login password
        driver.findElement(By.id("password"))
            .sendKeys("xyz12345");

        driver.findElement(By.className("signin-button"))
            .click();
    }
}

After adding this code, click on the ‘Run‘ button.

Click on the ‘Run’ button

Chrome Driver will start successfully and Chrome browser will open. It will automatically open the w3wiki login page and add details for login. If the details are valid, it will redirect to its website.

Conclusion

in conclusion, To automate login using Selenium WebDriver with Java, set up the environment with JDK, Eclipse, Selenium, and Chrome Driver. Create a Java project and class, add needed JAR files, and write the WebDriver code for the login purpose. Run the code to automate and verify the login process

Frequently Asked Questions on Selenium Program to Login to a Specific Web Page

How to go to a specific URL in Selenium?

The get() method will be used and the most commonly used method to open a URL in Selenium.

How do you open a web page in Selenium?

Pass the URL of the page in driver. get() method.

How to click on webpage using Selenium?

The click() method performs a click action on a web element.