Sunday, May 11, 2014

How to get the suggestion's text by google which come as dropdown while typing in the search box of google.

package BhanuSir;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
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.openqa.selenium.firefox.FirefoxProfile;

public class GoogleSearch {
    public static void main(String[] args) {
        System.out.println("Enter the word which u want to search in Google: ");
        Scanner in = new Scanner(System.in);
        String srchWord = in.nextLine();
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/");
       
        driver.findElement(By.name("q")).sendKeys(srchWord);
        //get the test suggested by google from the dropdown.
        List<WebElement> sugg = driver.findElements(By.xpath("//table[@class='gssb_m']/tbody/tr"));
        for(WebElement ele: sugg){
            System.out.println(ele.getText());
        }
        driver.close();
    }
}

No comments:

Post a Comment