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