How to automate Instagram login page using java in Selenium?
Last Updated :
30 Aug, 2024
Automating the Instagram login page using Java and Selenium WebDriver is a crucial task for testing and automating web interactions. Instagram, being one of the most popular social media platforms, requires a precise approach to logging in and interacting with its elements. In this guide, we will walk you through the steps to automate Instagram login using Java and Selenium, highlighting the essential code snippets and techniques for a successful login automation process.
By following this tutorial, you will learn how to handle login forms, manage dynamic web elements, and ensure a smooth automation experience for Instagram's login functionality. This approach is beneficial for QA testing, performance testing, and automated functional testing of web applications.
Prerequisites
We will be required 3 main things:
- Java Development Kit (JDK) installed.
- Browser Driver (e.g., ChromeDriver for Chrome).
- IDE like Eclipse or IntelliJ IDEA.
Dependencies Selenium Handling Drop-Downs using Java
We will be required to have dependencies for selenium and for that, we will add dependencies in pom.xml
pom.xml
Java
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>4.21.0</version>
</dependency>
Application.java
Java
package com.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class InstaLogin {
public static void main(String[] args) {
// Automatically setup ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
try {
// Open Instagram login page
driver.get("https://www.instagram.com/accounts/login/");
// Wait for the login page to load
Thread.sleep(3000); // Adding sleep to wait for the page to load
// Locate the username field and enter your username
WebElement usernameField = driver.findElement(By.name("username"));
usernameField.sendKeys("userName");
// Locate the password field and enter your password
WebElement passwordField = driver.findElement(By.name("password"));
passwordField.sendKeys("Password");
// Locate the login button and click it
WebElement loginButton = driver.findElement(By.cssSelector("button[type='submit']"));
loginButton.click();
// Wait for the potential redirect and page load
Thread.sleep(5000); // Wait to observe the result
// Check if the sidebar or any other element indicating a successful login is present
WebElement sideBar;
try {
sideBar = driver.findElement(By.cssSelector(".x1iyjqo2.xh8yej3"));
if (sideBar.isDisplayed()) {
System.out.println("Logged In");
} else {
System.out.println("Login Failed");
}
} catch (Exception e) {
System.out.println("Login Failed");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Note: Replace your Instagram Username and Password at field which need to be.
Output
Instagram Login OutputConclusion
Automating the Instagram login page using Java and Selenium WebDriver provides a powerful way to streamline testing and interaction with web applications. By following the steps outlined, you can efficiently handle dynamic elements, manage form submissions, and validate successful logins.
This approach not only enhances your testing efficiency but also ensures that your automation scripts can handle real-world scenarios effectively. Whether you're working on QA automation, performance testing, or functional testing, mastering these techniques will help you maintain robust and reliable automated test suites.
Similar Reads
How to Open a Browser in Headless Mode in Selenium using Java? Headless testing has become a significant trend in modern web automation, offering numerous advantages for developers and testers. In Selenium, running tests in headless mode allows browsers to operate without the graphical user interface (GUI). This article delves into the concept of headless brows
6 min read
How to click on an image using Selenium in Java? Selenium, a popular tool for automating web application testing across browsers, often requires image interaction. In this article we will discuss how to clicking on image using Selenium in Java.PrerequisitesJava Development Kit (JDK) installed.Selenium WebDriver library added to your project.A supp
3 min read
How to Run Opera Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
3 min read
How to Run Internet Explorer Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
6 min read
How to automate google Signup form in Selenium using java? For any QA engineer or developer, automating the Google Signup form with Selenium may be a hard nut to crack. Also, as the needs are increasing toward automated testing, in this article, we will learn how to deal with a complicated web form like Google Signup. We will show you how to automate the Go
4 min read
How to download File in Selenium Using java Automating the file download process is essential in web automation testing, especially for validating functionalities involving document downloads such as PDFs, images, or CSV files. However, Selenium WebDriver doesnât directly handle file downloads. To overcome this limitation, we can configure th
2 min read