Thursday, 19 June 2014

program for search a word in google search url

program for search a word in google search url


solution:-----

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.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class googlesearch {
WebDriver driver;
  @Test
  public void f() throws Exception {
 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);
      Thread.sleep(2000);
      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.id("gbqfq"));
      for(WebElement ele: sugg){
          System.out.println(ele.getText());
      }
  }
  @BeforeTest
  public void beforeTest() {
  }

  @AfterTest
  public void afterTest() {
  }

}


No comments:

Post a Comment