简体   繁体   中英

Java Selenium WebDriver: Click on Instagram Login Button

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);
    }
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM