WebDriver gives us “Find Element” and “Find Elements” method to locate elements on the web page.
By Name
WebElement element = driver.findElement(By.name("ss"));
By ID
WebElement element = driver.findElement(By.id("Username"));
By Class Name
WebElement element = driver.findElement(By.className(""));
By Link Text
WebElement element=driver.findElement(By.linkText("Link Name"));
By Tag Name
WebElement element=driver.findElement(By.tagName("dt"));
By Partial Link Text
WebElement element=driver.findElement(By.partialLinkText("Link"));
Practice Yourself :
package dayOne;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebElementsCommands {
public static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/Automation-practice-form/");
driver.findElement(By.partialLinkText("Partial Link Test")).click();
System.out.println("Link Test Pass");
String sClass = driver.findElements(By.tagName("button")).toString();
System.out.println(sClass);
driver.findElement(By.linkText("Link Test")).click();
System.out.println(" Test Pass");
}
}