Id Name and Class Name

By.id and By.name

WebElement email = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.name("password"));
email.sendKeys("[email protected]");
password.sendKeys("Test@123");

id should be unique on the page, so it is the fastest and most reliable locator. name is common on form fields.

By.className

// <button class="btn btn-primary">  has TWO classes: btn and btn-primary
driver.findElement(By.className("btn-primary"));    // OK: one class name
driver.findElement(By.className("btn btn-primary")); // fails: compound class names are not allowed

Example

The second line throws:

Run ClassNameTest.main()

Example output

org.openqa.selenium.InvalidSelectorException: invalid selector: Compound class names not permitted

Tip: For several classes use CSS: By.cssSelector(".btn.btn-primary").