WebElement Interface Hierarchy:
Before going into depth, how to find the element in selenium. First, we will have a look at the basic architecture of the WebElement interface. It extends SearchContext and TakesScreenShot interfaces. It has many useful abstract methods like click, sendKeys, isSelected(), etc. Given below is a basic architecture of WebElement interface.Find Element, or Elements command is used to interact with the web elements. If you want to find a single element, then you will have to use Find Element command, but for more than one element Find Elements command is used.
There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, CSSSelector, Link Text, Partial Link Text, Tag Name, and XPath, etc.
FindElement Command Syntax:
Find Element command takes an object "By" as a parameter and returns an object of type WebElement.By object can be used with various locators like ID, Name, Link Text, Class Name, Tag Name, Xpath, and CSSSelector. Find Element method returns a single WebElement. Suppose we wanted to find an element by the "id" attribute, the command will be written as given belowWebElement element = driver.findElement(By.id("id_value"));Following locators can be used in Selenium WebDriver:
- ID Name
- Class Name
- CSSSelector
- Tag Name
- Link Text
- Partial Link Text
- XPath
Example:
WebElement loginName = driver.findElement(By.xpath("//input[@type= 'email']"));
FindElements Command Syntax:
Find Elements command also takes an object "By" as a parameter and returns a list of web elements. It returns an empty list if there are not elements found using the given locator type and value. Below is the command syntax of find elementsListExample:elementName = driver.findElements(By.LocatorType("LocatorValue"));
ListlistOfElements = driver.findElements(By.xpath("//button[@type='button']"));
Working Example of FindElement:
- Go here https://accounts.google.com/
- Find the text field to enter email
- Enter email
package learning; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class FindElement { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.get("https://gmail.com"); WebElement username = driver.findElement(By.xpath("//input[@type= 'email']")); username.sendKeys("test@gmail.com"); //driver.close(); } }The above code will do the following things:
- Will open the specified URL
- Will locate the email field
- Will enter the email into the text field
Working Example of FindElements:
- Open the URL under test
- Find all the buttons
- Print the tag name
package learning; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class FindElements { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.get("https://www.google.com/intl/en-GB/gmail/about/"); ListOutput:list = driver.findElements(By.xpath("//button[@type='button']")); System.out.println("Number of elements:" + list.size()); for (int j = 0; j < list.size(); i++) { System.out.println("Element "+i+" Tag Name:" + list.get(i).getTagName()); } } }
Element 0 Tag Name:button Element 1 Tag Name:button Element 2 Tag Name:button Element 3 Tag Name:button Element 4 Tag Name:button Element 5 Tag Name:button Element 6 Tag Name:button Element 7 Tag Name:button Element 8 Tag Name:button
Method Detail:
findElements
Find a list of web elements within the web page.Parameters: The object reference variable of WebElement Interface
Returns: A list of all WebElements.
Find the first web element within the web page.
Parameters: By class, the object is passed as a parameter
Returns: The object reference variable of WebElement Interface
Throws: NoSuchElementException - If no matching element is not found
Conclusion:
Find Element command returns a single web element Find Elements command returns a list of web element Find Element command throws NoSUchElementFoundException if it does not find the matching element Find the Elements command returns an empty list if there are no matching elements.