FindElement command syntax:
WebElement elementName = driver.findElement(By.LocatorStrategy(“LocatorValue”)); Selenium Find Element command takes in the By object as the parameter and returns an object of type list WebElement in Selenium. By object in turn can be used with various locator strategies such as find element by ID Selenium, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in Selenium web driver. Locator Strategy can be any of the following values.
ID Selenium find element by Name Class Name Tag Name Link Text Partial Link Text XPATH
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. Example: WebElement loginLink = driver.findElement(By.linkText(“Login”));
Example: Find Element in Selenium
The following application is used for demo purpose http://demo.guru99.com/test/ajax.html
Scenario:
Step 1: Open the AUT Step 2: Find and click radio button
FindElements command syntax:
List
Example: Find Elements in Selenium
Scenario:
Step 1: Open the URL for Application Under Test Step 1: Find the text of radio buttons and print it onto the output console
Find element Vs Find elements
Below are the major differences between find element and find elements commands.
Summary:
Find Element command returns the web element that matches the first most element within the web page. Find Elements command returns a list of web elements that match the criteria. Find Element by XPath in Selenium command throws NoSuchElementException if it does not find the element matching the criteria. Find Elements command returns an empty list in Selenium if there are no elements matching the criteria