Coding with Java

Single Element Locators

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.

 

// ELEMENTS WebElement myElement_textBox = driver.findElement(By.id("your_name")); WebElement myElement_button = driver.findElement(By.id("submit")); WebElement myElement_label = driver.findElement(By.cssSelector(".HeaderCampaign")); WebElement myElement_select = driver.findElement(By.id("cities")); // Add this line to TestCase's Hook Header Code Section import java.util.List; // Step Code List<WebElement> myElement_list = driver.findElements(By.tagName("li")); // ELEMENT ACTIONS myElement_button.click(); myElement_textBox.clear(); myElement_textBox.sendKeys("String"); String myLabelText = myElement_label.getText(); String myButtonValue = myElement_button.getAttribute("value"); Integer myElementSize = myElement_list.size(); Boolean isElementDisplayed = myElement_label.isDisplayed(); Boolean isElementDisplayed = myElement_label.isEnabled(); Boolean isElementDisplayed = myElement_label.isSelected(); // Add this line to TestCase's Hook Header Code Section import org.openqa.selenium.support.ui.Select; // Step Code Select select = new Select(myElement_select); select.selectByVisibleText("İstanbul"); select.deselectByVisibleText("İstanbul"); select.selectByValue("ANKARA"); select.deselectByValue("ANKARA"); select.selectByIndex(2); select.deselectByIndex(2);

 

OTHERs:

 

 

Related Links: