Open In App

Selenium Program to Login to a Specific Web Page

Improve
Improve
Like Article
Like
Save
Share
Report

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 programmings languages such as Java, Python, Ruby, C#, JavaScript, Perl, and PHP. Here we are using Java for writing test scripts.

Tip: One should be well versed with Eclipse and JDK understanding concepts.

Procedure:

  • Create New Project, Click on File
  • Create a new java class
  • Download the latest Chrome Driver and Selenium Web Driver for Java
  • Click on Classpath and Select Add External JARs.

These above steps are as depicted below with help of visual aids for better understanding. 

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

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.

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.

Step 4: Click on Classpath

4.1: Initially alongside select Add External JARs.

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

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

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

Example

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.geeksforgeeks.org/");
  
        // 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.

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



Last Updated : 30 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads