Sunday, 15 September 2013

Store Auto-suggestions using iterator | Webdriver

Identifying auto-suggestion lists and storing them can be achieved using iterator. Couple of examples shown below demonstrate iterators and their use on auto-complete.

e.g.,
1|
driver.get(baseUrl+"http://www.indiabookstore.net/");
driver.findElement(By.id("searchBox")).sendKeys("sam");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
WebElement table = driver.findElement(By.className("acResults"));
List<WebElement> rowlist = table.findElements(By.tagName("li"));
System.out.println("Total No. of list: "+rowlist.size());
Iterator<WebElement> i = rowlist.iterator();
System.out.println("Storing Auto-suggest..........");
while(i.hasNext())
{
  WebElement element = i.next();
  System.out.println(element.getText());
}


2|
driver.get(baseUrl+"https://www.google.co.in/");
driver.findElement(By.id("gbqfq")).sendKeys("Sams"); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
   
WebElement table = driver.findElement(By.className("gssb_m"));     
List<WebElement> rows = table.findElements(By.tagName("span")); 
System.out.println("Total no. of rows: "+ rows.size());
Iterator<WebElement> i = rows.iterator();  
while(i.hasNext()) {  
  WebElement row = i.next();  
  System.out.println(row.getText());

Thread.sleep(4000);
rows.get(1).click();
Thread.sleep(5000);



Alternate Concept for iteration


List<WebElement> listitems = driver.findElements(By.id("value"));
for(WebElement temp: listitems) // temp is the declared variable name
{
System.out.println((temp.findElement(By.tagName("value")).getText()));   
}

1 comment: