Find Element

If we want to perform any operation on webelement, we have to find them using locators but locators finds 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 help of By class methods(nothing but locators).

Handle dropdowns in selenium

Things to remember when you are using Find Element


1. Use this method to access any single element on the web page.

2. It returns the first matching element of the specified locator.

3. It throws an NoSuchElementException exception when it fails to locate the element( when element is not there).

Syntax : WebElement example = driver.findElement(By.locator("locator")); 

My Experience with findElement()


1. You cannot get other than first element using findElement()

2. Implicitly Wait applicable on findElement()

3. 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 findElement() method to find single element but sometime we may want to perform 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


1. We don’t use it frequently

2. 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.

3. If the element doesn’t exist or not available on the page then, the return value will be an empty list.

4.It will not throw 'NoSuchElementFoundException' in case element is not present

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

My Experience with findElements()


1. We can use findElements to mimic findElement(), example covered below

2. You can retrieve elements as List or ArrayList.

3. User will be allowed to perform only list operations on the return value, we have to iterate through each item to perform webelement operations

4. implicitlyWait applicable on findElements on special occasion, please visit implicitlyWait for more details

5. 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 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