as

Monday, 1 June 2015

How to get search queries for given keyword from Google by Automation

Here is the sample program to get the list of search queries related to given keyword.
package framework;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;
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;
import org.openqa.selenium.remote.DesiredCapabilities;

public  class Google {
   
   
   public static void main(String[] args) {
   
         WebDriver driver =null;
         
   System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
   System.out.println(System.getProperty("webdriver.chrome.driver"));
   driver = new ChromeDriver();
   
  // driver = new ChromeDriver(DesiredCapabilities.chrome());      driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
   driver.manage().window().maximize();
   
   try{
      
driver.get("https://www.google.co.in/preferences?hl=en-IN&fg=1");  
driver.findElement(By.xpath("//div[text()='Never show Instant results.']")).click();
driver.findElement(By.xpath("//div[text()='Save']")).click();

driver.get("https://www.google.co.in");       

//String x = driver.switchTo().frame(0).switchTo().frame(0).findElement(By.tagName("h3")).getText();//String x = driver.switchTo().frame(1).findElement(By.tagName("h3")).getText();//String x = driver.switchTo().frame("view").switchTo().frame(0).getPageSource();//System.out.println(x);
String str = "windows 7 How to ";
String d = "";
   
for (int i=1 ;i<=26;i++)
   
{
   char x = (char) (64+i);
   
driver.findElement(By.cssSelector("input[name=q]")).sendKeys(str + x);

Thread.sleep(2000);
List <WebElement> tr =  driver.findElements(By.xpath("//table[@class='gssb_m']//tr"));

for (int co=0 ; co<tr.size();co=co+2)
{
   
   d= d + tr.get(co).getText() + "\r\n";
}

driver.findElement(By.id("lst-ib")).clear();

}

System.out.println(d);
String filePath = "f:\\videos\\"+ str +".txt";
               
               if ((new File(filePath)).exists() )
                  (new File(filePath)).delete();
               
               
               File temp = new File(filePath);
               FileWriter fw = new FileWriter(temp,true);
               fw.append(d);
               fw.close();
               


 

            Thread.sleep(4000);
            //driver.navigate();//driver.navigate("http://www.google.com");      }catch(Exception e){

   //System.out.println(e.getLocalizedMessage());                  e.printStackTrace(new PrintStream(System.out));
         
      }
         
         
      finally{
         driver.close();
         driver.quit();
      }  
         
      
      
      
      
   }
   
   

   
}


What do you think on this topic? Please express your opinion or ask any question through comment below. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Leave your valuable feedback. Your thoughts do matter to us.

Sponsored Links

Popular Posts

Comments

ShareThis