Chapter 5:Find element/elements in selenium and Driver Functions

Why do you need Find Element/s command?

Fundamental interaction with a web page requires a user to locate the web element. Find Element command is used to uniquely identify a (one) web element within the web page. Whereas, Find Elements command is used to uniquely identify the list of web elements within the web page. There are multiple ways to uniquely identify a web element within the web page such as Name, ID, Link Text, Class Name, Partial Link Text, Tag Name and XPATH.

FindElement command syntax:

Find Element command takes in the By object as the parameter and returns an object of type WebElement. By object in turn can be used with various locator strategies such as ID, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in Selenium web driver.

Webelement elementname=driver.findElement(By.Locator Stratagy(“locatorValue”));

Locator Strategy can by any of the following values.

  • Link Text
  • XPATH
  •  Tag Name
  • Class Name
  • ID
  • Partial Link Text
  •  name

Locator Value is the unique value using which a web element can be identified. It is the responsibility of developers and testers to make sure that web elements are uniquely identifiable using certain properties such as ID or name.

For Example:

The following application is used for demo purpose

https://phptravels.com/demo/

Script:

1. Open the URL

2. Find and click on demo link

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class NameDemo 
  {
	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://phptravels.com/demo/");
       // find the demo link using linktext and click on it
      WebElement demolink = driver.findElement(By.linkText("DEMO"));
		demolink.click();
		String CurrentPageTitle = driver.getTitle();
		System.out.println(CurrentPageTitle);
	}
}

FindElements command syntax:

Find Elements command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value. Below is the syntax of find elements command.

List<WebElement> elementsName=driver.findElements(By.LocatorStrtagy(“LocatorValue”));

For Example:

Script:

1. Open the URL for Application Under Test

2. Fill the railway junctions and print the list of trains on console output.

package testology;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class nameDemo1 {
	static WebDriver dr = null;
    public static void main(String[] args) throws InterruptedException 
{
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		dr = new ChromeDriver();
		dr.manage().window().maximize();
		dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		dr.get("https://erail.in/");
		// to get the src of railway junctions by using their id
		WebElement sr = dr.findElement(By.id("txtStationFrom"));
		sr.clear();
		sr.sendKeys("Pune Jn");
		// to get the src address as pune station using css selector
		WebElement src = dr.findElement(By.cssSelector("div[class = 'autocomplete'][id *= 'Autocomplete']"))
				.findElement(By.cssSelector("div[title *='Pune Jn']"));
		src.click();
		// to get the destination of railway junctions by using their id
		WebElement des = dr.findElement(By.id("txtStationTo"));
		des.clear();
		// to get the destination address as kerala station
		des.sendKeys("Kerala" + Keys.ENTER);
		// to get train type as 3TierAc
		WebElement teir = dr.findElement(By.id("selectClassFilter"));
		Select traintype = new Select(teir);
		traintype.selectByValue("2");
		WebElement gettrain = dr.findElement(By.id("buttonFromTo"));
		gettrain.click();
		Thread.sleep(3000);
		// to get lists of all trains after clicking on gettrainsbutton using css
		// selector
		List<WebElement> allrows = dr.findElements(By.cssSelector("#divTrainsList>table>tbody>tr"));
		List<String> alltrainslist = new ArrayList<String>();
		for (WebElement temp : allrows) {
			System.out.println(temp.getText());
			if (temp.findElements(By.tagName("td.")).size() > 0) 
                   {
String trainNumber = temp.findElement(By.tagName("td")).getText();
				alltrainslist.add(trainNumber);
			}
		}
		System.out.println(alltrainslist);
	}
}

See above video to understand more about driver function and about how to find element through parent element.