Whenever you want to interact with a web page, we require a user to locate the web elements. We usually start by finding the HTML elements on the page whenever we plan to automate any web application using WebDriver. Selenium WebDriver defines two methods for identifying the elements, they are findElement
and findElements
.
There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, LinkText, PartialLinkText, TagName, and XPath.
FindElement() Method:
FindElements() Method:
Find Element command takes in the By object as a parameter and returns an object of type WebElement. By object can be used with various locator strategies such as ID, Name, ClassName, link text, XPath, etc.
WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator Strategy can be any of the following values.
Locator Value is the unique value using which we can identify the web element. It is the core responsibility of developers and testers to make ensure that web elements are uniquely identified by using certain properties such as ID or Name. Example:
WebElement login= driver.findElement(By.linkText("Login"));
Selenium findElements command takes in By object as the parameter and returns a list of web elements. It returns an empty list if no elements found using the given locator strategy and locator value.
List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
Example:
List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
The following application is used for demo purpose: https://www.irctc.co.in/nget/user-registration
Scenario
package com.journaldev.selenium.findelement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumFindElement {
public static void main (String [] args){
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window.maximize():
driver.get(:"https://www.irctc.co.in/nget/user-registration");
//Find the radio button for "Male" by using ID and click on it
driver.findElement(By.id("M")).click();
}
}
The following application is used for demo purpose https://www.irctc.co.in/nget/user-registration
Scenario
package com.journaldev.selenium.findelements;
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 SeleniumFindElements {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.irctc.co.in/nget/user-registration");
List<WebElement> elements = driver.findElements(By.id("M"));
System.out.println("Number of elements:" +elements.size());
for(int i=0; i<elements.size(); i++){
System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
}
}
}
Selenium Webdriver references the web elements by using findElement(By.)
method. The findElement method uses a locator object known as <"By">
. There are various kinds of “By” strategies which you can use depending on your requirement.
Command: driver.findElement(By.id(<element ID>)) Example: <input id=“JournalDev”> Java example code to find the input element by id
WebElement user = driver.findElement(By.id("JournalDev"));
Command: driver.findElement(By.name(<element-name>)) Example: <input name=“JournalDev”> Java example code to find the input element by name
WebElement user = driver.findElement(By.name("JournalDev"));
Command: driver.findElement(By.className(<element-class>)) Example: <input class=“JournalDev”> Java example code to find the input element by className.
WebElement user = driver.findElement(By.className("JournalDev"));
Command: driver.findElement(By.linkText(<link text>)) Example: <a href=“#test1”>JournalDev-1</a> <a href=“#test2”>JournalDev-2</a> Java example code to find element matching link or partial link text:
WebElement link = driver.findElement(By.linkText("JournalDev-1"));
WebElement link = driver.findElement(By.partialLinkText("JournalDev-2"));
Command: driver.findElement(By.cssSelector(<css-selector>)) Example: <input class=“email” id=“email” type=“text” placeholder=“xxx@email.com”> <input class=“btn btn-small” type=“submit” value="Subscribe to blog> Java example code to find element matching link or partial link text:
WebElement emailText = driver.findElement(By.cssSelector("input#email"));
Command: driver.findElement(By.xpath(<xpath>)) Java example code for XPath:
// Absolute path
WebElement item = driver.findElement(By.xpath("html/head/body/table/tr/td"));
// Relative path
WebElement item = driver.findElement(By.xpath("//input"));
// Finding elements using indexes
WebElement item = driver.findElement(By.xpath("//input[2]"));
```****
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.