Find Element in selenium

If we want to perform any operation on webelement, we have to find them using locators, but locators find them on the browser, to make those elements available to selenium we have to find them with selenium Method.

Selenium provides method 'findElement()'. This method finds the element with the help of By class methods(nothing but locators).

Handle dropdowns in selenium

Things to remember when you are using Find Element

  • Use this method to access any single element on the web page.
  • It returns the first matching element of the specified locator.
  • It throws a NoSuchElementException exception when it fails to locate the element( when an element is not there).
Syntax : WebElement example = driver.findElement(By.locator("locator"));

My Experience with findElement():

  • You cannot get other than first element using findElement()
  • Implicitly Wait applicable on findElement()
  • If you are in page level, findElement() will look for the element in page level; it will not look for the element which is present inside a Frame and vice versa
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FindEle {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://chercher.tech/practice-page");
		// className locator
		driver.findElement(By.className("className")).click();
		// css locator
		driver.findElement(By.cssSelector(".className")).click();
		// id  locator
		driver.findElement(By.id("cn")).click();
		// link text locator
		driver.findElement(By.linkText("selenium webdriver")).click();
		// partial link text locator
		driver.findElement(By.partialLinkText("webdriver")).click();
		// tagname locator
		driver.findElement(By.tagName("a")).click();
		// xpath locator
		driver.findElement(By.xpath("//a")).click();
	}
}

Take Web Element Screenshot in selenium

Find Elements

We can use the findElement() method to find a single element, but sometime we may want to perform an operation on more than one element, in those cases, we cannot write findElement() method for all the elements.

In selenium we have a findElements() method which can return multiple elements matching with given locator, findElements() returns List<WebElement>.

Note : findElements() returns List<WebElement> not WebElements

Video tutorial for Find Elements

Subscribe to my youtube channel :

Things to remember when you are using Find Elements:

  • We don’t use it frequently
  • It gives back the whole list of all(not all but all the elements which are loaded by that time) the elements matching the specified locator.
  • If the element doesn’t exist or not available on the page, then the return value will be an empty list.
  • It will not throw 'NoSuchElementFoundException' in case element is not present

Syntax : List<WebElement> elems = driver.findElements(By.locator("locator"));

My Experience with findElements():

  • We can use findElements to mimic findElement(), example covered below
  • You can retrieve elements as List or ArrayList.
  • User will be allowed to perform only list operations on the return value; we have to iterate through each item to perform webelement operations
  • implicitlyWait applicable on findElements on a special occasion, please visit implicitlyWait for more details
  • If you are in page level, findElements() will look for the element in page level; it will not look for the element which is present inside a Frame and vice versa
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FindElems {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://chercher.tech/practice-page");

		// xpath locator
		List<WebElement> elems = driver.findElements(By.xpath("//input[@id='all']"));
		// go through one by one element in elems list
		for (int i = 0; i < elems.size(); i++) {
			elems.get(i).click();
		}
	}
}

Mimic findElement() using findElements()

In below code examples we will see how to select the first checkbox in Selenium Webdriver Practice Page page using findElement and findElements

// findElement Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FindElem {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://chercher.tech/practice-page");

		// select first checkbox
		driver.findElement(By.xpath("//input[@id='all']")).click();
	}
}


With findElements()

// findElements Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FindElems {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://chercher.tech/practice-page");

		// select first checkbox
		driver.findElements(By.xpath("//input[@id='all']")).get(0).click();
	}
}
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions