Showing posts with label find elements example in selenium web driver. Show all posts
Showing posts with label find elements example in selenium web driver. Show all posts

Sunday 19 May 2019

Selenium Web Driver Tutorials - Dynamic Search Example

In this tutorial we will see how to perform dynamic search using Selenium web Driver 

For example i am taking  tsrtc(https://tsrtconline.in/oprs-web/) site .here if you type from place few characters it will gives you the suggestions bar from suggestions bar we need to select the from place. I have automated this scenario using selenium web driver.

First we need to identify the from place web element ,then using sendKeys in selenium we need to enter data (in our case I am sending "Banga") after half second it will gives you the suggestion bar.
With findElements we can get the list of elements in suggestion bar,using for loop and getText() in selenium web driver we can get all the elemenents .Using simple if else condition we can desired element .

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DynamicSearch {
 public static void main(String[] args) throws InterruptedException {
String path ="F:\\Automation_lib\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",path);
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://tsrtconline.in/oprs-web/");
 
WebElement fromPlace = driver.findElement(By.id("fromPlaceName"));
fromPlace.sendKeys("Banga");
Thread.sleep(1000);
 
List<WebElement> suggestions = driver.findElements(By.xpath("//ul[@id='ui-id-1']/li"));
 
for(WebElement el : suggestions) {
System.out.println(el.getText()+"***");
if(el.getText().trim().equals("BANGALORE")) {
el.click();
break;
}
}
 
 
 
 }
}


For video version tutorial please refer below




Friday 17 May 2019

Find Elements Example In Selenium Web Driver

In this tutorial we will learn find Elements example in selenium web driver

findElements in selenium will return the list of web elements if match found for all the elements for a given selector. If no match found it will return NULL unlike findElement will return element not found exception.

So below example will show you how to use findElements and how to loop through the list of web elements .



import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CountOfLinks {
 public static void main(String[] args) {
String path ="F:\\Automation_lib\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",path);
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.icicibank.com/");
 
List<WebElement> els =driver.findElements(By.tagName("a"));
System.out.println("Number of links :"+els.size());
for(WebElement e :els) {
System.out.println(e.getAttribute("href")+"*****"+e.getText());
}
 
 }
}