Selenium findElement & findElements Examples

Web page interaction needs the driver to find a web element, & either initiate a JavaScript event including a click, select enter, etc., or key in the field value. Typically, one begins automated testing of a web app by finding significant web elements on the web page. This kind of automated testing is often executed with the help of frameworks including Selenium WebDriver.

The find Element command is utilized to distinctly identify a single web element in a web page. While the Find Elements command is utilized to distinctly find the list of web elements on a web page. Moreover, there are numerous methods to uniquely find a web element on the web page. These include ID, Class Name, Name, Link Text, Tag Name, Partial Link Text, and XPATH.

syntax  of the FindElement command

The findElement command gives an object of the variety WebElement. It can utilize different locator strategies such as Name, ID, ClassName, XPath, link text, etc. Its syntax is

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

Locator Value is a distinct value which a web element could be identified with. It’s the responsibility of developers plus testers to ensure that web elements are distinctively identifiable utilizing certain properties like the name or ID.

Example

WebElement login= driver.findElement(By.linkText("Login"));

Selenium findElements Command

It takes in by object as a parameter & returns several web elements. However, you will have an empty list if there are no elements found utilizing the given locator strategy plus locator value.

FindElements Command Syntax

List<WebElement> elementName = driver.findElements(By.LocatorStrategy(“LocatorValue”));

An Example

List<WebElement> listOfElements = driver.findElements(By.xpath(“//div”));

Using Selenium findElement

Find by Name

This is just like Find By ID excluding the driver will find an element by the “name” attribute in place of “id”.

WebElement user = driver.findElement(By.name(“JournalDev”));

Find by ID

ID is distinctly defined for every element and is the more common method of locating elements with the help of the ID Locator. If a site has dynamically generated ids, then the strategy can’t be utilized to find an element uniquely. Moreover, it will give the first web element that matches the locator.

WebElement user = driver.findElement(By.id("JournalDev"));

Find By LinkText

LinkText assists in finding links in a webpage. It’s the most effective method of finding web elements that contain links.

WebElement link = driver.findElement(By.linkText(“JournalDev-1”));

WebElement link = driver.findElement(By.partialLinkText(“JournalDev-2”));

Find By XPath

XPath is a method of using selenium to navigate via a page’s HTML structure. XPath allows testers to navigate via the XML structure of every document & can be utilized on both XML and HTML documents.

elementxpath = driver.findElement(By.xpath("//input[@id=’twotabsearchtextbox’]"))

Find By CSS Selector

CSS Selector is utilized to offer style rules for web pages & also can be utilized to identify single or multiple web elements.

WebElement emailText = driver.findElement(By.cssSelector("input#email"));

By Class Name

Command: driver.findElement(By.className(<element-class>)) For instance: <input class=“JournalDev”> A Java code to get all the input elements by className.

WebElement user = driver.findElement(By.className("JournalDev"));

Leave a Comment