My Thoughts: Getting google search auto suggestions using Webdriver(Selenium 2)

Monday, May 28

Getting google search auto suggestions using Webdriver(Selenium 2)

In the google search when we start typing any search query google will start auto suggestions. All these search suggestions are part of a WebTable. If we would like to capture all these search suggestions then we have to just iterate through the table.

Here is the sample code which will start typing "vam" and then capture all search suggestions .
import java.util.Iterator;
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.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SearchSuggestion {
 
WebDriver driver;
 
 @BeforeTest
 public void start(){
   driver = new FirefoxDriver(); 
 }
  
 @Test
  public void SearchSuggestion() {
  
  driver.get("http://google.com");
  driver.findElement(By.id("gbqfq")).sendKeys("vam");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
   WebElement table = driver.findElement(By.className("gssb_m")); 
   List rows = table.findElements(By.tagName("tr")); 
   Iterator i = rows.iterator(); 
   System.out.println("-----------------------------------------"); 
   while(i.hasNext()) { 
           WebElement row = i.next(); 
           List columns = row.findElements(By.tagName("td")); 
           Iterator j = columns.iterator(); 
           while(j.hasNext()) { 
                   WebElement column = j.next(); 
                   System.out.println(column.getText()); 
           } 
           System.out.println(""); 
            
   System.out.println("-----------------------------------------"); 
   } 
  } 
}

Here is what we will see in the browser after running the above code.




5 comments:

  1. This post helped. Thanks!

    ReplyDelete
  2. Thanks for using the tag names instead of the xpath in the example, this helped me in solving an automation issue related to suggestions.

    ReplyDelete
  3. Hi Vamshi!

    Nice Posting. The above code needs slight changes. Pls address here.....

    driver.get("https://www.google.co.in/");
    driver.findElement(By.id("gbqfq")).sendKeys("vam");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    WebElement table = driver.findElement(By.className("gssb_m"));
    List rows = table.findElements(By.tagName("span"));
    System.out.println("Total no. of rows: "+ rows.size());
    Iterator i = rows.iterator();
    while(i.hasNext()) {
    WebElement row = i.next();
    System.out.println(row.getText());
    }

    _

    Sams

    http://seleniumworks.blogspot.in/

    ReplyDelete
  4. Again searching for Auto suggestion program and found u r blog ...!and successfully run the program just in 1 hit.
    @vamshi u r really a genius :) 3 claps 4 u .....!

    ReplyDelete
  5. Your information about Selenium scripts is really
    interesting. Also I want to know the latest new techniques which are
    implemented in selenium. Can you please update it in your website?

    Selenium
    training Chennai

    ReplyDelete