0
votes

I am new to coding and the Selenium WebDriver and I cannot figure out how to automate a login process for Instagram. I figured out how to input the username and password, but I was unable to figure out how to click on the login button.

Here is my code:

package com.company;

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

import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","chromedriver");

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.findElement(By.name("username")).sendKeys("username");
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.xpath("//button[contains(@class, 'loginBtn')]")).click();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
}
1
Hi, welcome to stack overflow. What kind of error do you encounter? - Jeroen Heier
I am attempting to click the login button for Instagram but I was unable to figure it out for almost a month now. Any guidance on how to click the login button would be appreciated - jrdnangin
What about this? - Jeroen Heier
That tutorial is using Python. However, I want to build the program in Java - jrdnangin

1 Answers

1
votes

The class for the login button is dynamic so you cannot click on it using the classname. However, you can click it using the text of the button in the xpath and I have verified it by running it myself.
You can do it like:

driver.findElement(By.xpath("//div[text()='Log In']")).click();