Selenium uses what is called locators to find and match the elements of your page or app that it needs to interact with.
WebElement myElement_name = driver.findElement(By.id("your_name"));
WebElement myElement_age = driver.findElement(By.cssSelector("a.css-yourage > span"));
WebElement myElement_submit = driver.findElement(By.xpath("//html[1]/body[1]/div[1]"));
myElement_name.clear();
myElement_name.sendKeys("John Smith");
myElement_age.sendKeys("25");
myElement_submit.click();
//This code is commented
// Another option that doing the same thing
driver.findElement(By.id("your_name")).clear();
driver.findElement(By.id("your_name")).sendKeys("John Smith");
driver.findElement(By.cssSelector("a.css-yourage > span")).sendKeys("25");
driver.findElement(By.xpath("//html[1]/body[1]/div[1]")).click();
//All of the available Element finders.
driver.findElement(By.id("visibleButtonTest")).click();
driver.findElement(By.className("header-text")).click();
driver.findElement(By.name("save")).click();
driver.findElement(By.tagName("a")).click();
driver.findElement(By.linkText("submit")).click();
driver.findElement(By.partialLinkText("goback")).click();
driver.findElement(By.cssSelector(".header-text")).click();
driver.findElement(By.xpath("//span[text()='Save']")).click();
Locate Multiple Elements
One may want to select a group of elements and then iterate through them.
//findElements is List<WebElement>
//get(0) means the 1st element of the list, get(1) 2nd element of the list..
//You should combine many of the findElement or findElements if you need.
driver
.findElements(By.tagName("ul")).get(2)
.findElements(By.tagName("li")).get(0)
.click();
// Another option that doing the same thing
// Add this line to TestCase's Hook Header Code section that needs to be imported first to use
import java.util.List;
//Step Code
List<WebElement> myElement_list = driver.findElements(By.tagName("li"));
myElement_list.get(0).click();
Element Actions / Operations
Actions like clicking a button, entering a keyword in the search bar are prime examples of how we use a mouse or keyboard.