Search code examples
seleniumauthenticationwebdriverclassname

How to enter username and password in Facebook using class names


After doing lot of R&D on google and StackOverflow i am posting this query.. i want to enter the username and password on Facebook using ONLY class name, I dont want to use type, name, id for this.. i tried with all possible options but no luck can someone provide the solution for this.. here is my code..

package login;

import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.*;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class MercuryLogin1 {

  private WebDriver driver;

  private String baseUrl;

  @BeforeClass(alwaysRun = true)

  public void setUp() throws Exception {

    System.setProperty("webdriver.chrome.driver","C:\\Automation_Info\\Selenium\\chromedriver_win32\\chromedriver.exe");

    System.setProperty("webdriver.gecko.driver", "C:\\Automation_Info\\Selenium\\geckodriver-v0.9.0-win64\\geckodriver.exe");

    driver = new ChromeDriver();

    baseUrl = "http://facebook.com/";   

    //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

  @Test

  public void testMercuryLogin1() throws Exception {

    driver.get(baseUrl + "/");

    driver.findElement(By.xpath("//input[@class=inputtext][0]")).sendKeys("tutorial");

    driver.findElement(By.xpath("//input[@class='nputtext][1]")).sendKeys("tutorial");

  }

  @AfterClass(alwaysRun = true)

  public void tearDown() throws Exception {

    //driver.quit();

   }

}

Solution

  • If you want to enter user name and password using their class, try using below By.xpath() :-

    public void testMercuryLogin1() throws Exception {
    
        driver.get(baseUrl + "/");
    
        WebDriverWait wait = new WebDriverWait(driver, 10);
    
        WebElement user = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(.//input[@class = 'inputtext'])[1]")))
    
        user.sendKeys("tutorial");
    
        WebElement pass = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(.//input[@class = 'inputtext'])[2]")))
    
        pass.sendKeys("tutorial");
    
      }
    

    Note :- I'm not sure why are you going to enter value using class Name while it could be simply enter by using By.id("email") and By.id("pass"). I'm suggesting you from the performance perspective using By.id is much faster than other locator. So make it priority first if it is possible.

    Hope it helps...:)