Skip to main content

Web Element(s) in Selenium WebDriver

In our previous post we have seen WebDriver Methods, In this post, we will discuss about Find Element(s) commands. These commands are very useful in Selenium Web Driver programming!

Every method of the WebDriver either returns something or return void(means return nothing). The same way findElement method of WebDriver returns WebElement.

By XPath: 

xpath(String xpathexpression) : By – It is most popular and majorly used locating element technique or the easiest way to locate element in WebDriver. It takes a parameter of String which is a XPATHEXPRESSION and it returns a BY object to findElement() method.

Command – driver.findElement(By.xpath(“Element XPATHEXPRESSION”));

The best thing in xpath is that it provides many different technique to locate elements. It gives you feature to locate single element in many ways.

Please see the below example:
Program on  number of radio buttons:

package program.practice;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class countradiobuttons {
public static void main(String[] args) {
FirefoxDriver d= new FirefoxDriver();
d.get(“http://spicejet.com/”);
List<WebElement>radiobuttons=d.findElements(By.xpath(“//input[@type=’radio’]”));
System.out.println(“no of radio buttons”+” ” + radiobuttons.size());
}
}

By tagName:

Command-driver.findElement(By.tagName(“Element TAGNAME”));

Program to find number of images:

package program.practice;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class image {
public static void main(String[] args) {
FirefoxDriver d= new FirefoxDriver();
d.get(“http://spicejet.com/”);
List<WebElement>images=d.findElements(By.tagName(“img”));
System.out.println(“no of images” + ” ” + images.size());
}
}

By id:

Command-Command – driver.findElement(By.id(“Element ID”));

Program on Drop Down list

package program.practice;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class checkbox {
public static void main(String[] args) {
FirefoxDriver d= new FirefoxDriver();
d.get(“http://spicejet.com/”);
d.findElement(By.id(“ctl00_mainContent_ddl_Adult”)).sendKeys(“5 Adults”);
d.findElement(By.id(“ctl00_mainContent_ddl_Child” )).sendKeys(“3 Children”);
}
}

In summary,we can use web elements commands in selenium webdriver, in the below syntax-

Id: WebElement element = driver.findElement(By.id(“”));

Name: WebElement element = driver.findElement(By.name(“”));

Tag Name: WebElement frame = driver.findElement(By.tagName(“iframe”));

Xpath: WebElement element = driver.findElement(By.xpath.name(“”));

CSS: WebElement element = driver.findElement(By.CSS.name(“”));

LinkText: WebElement element = driver.findElement(By.LinkText.(“”));

ClassName:WebElement element = driver.findElement(By.ClassName.(“”));

PartialLinkText:WebElement element = driver.findElement(By.PartialLinkText.(“”));

FindElements_03