Tuesday, 28 October 2014

How to handle google auto capture


 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class GoogleAutoCap {  
  public static void main(String[] args) throws InterruptedException {  
  // TODO Auto-generated method stub  
  WebDriver driver = new FirefoxDriver();  
  driver.get("http://www.google.com");  
  driver.findElement(By.id("gbqfq")).sendKeys("selenium");  // Entering values into the search box
  Thread.sleep(2000);  
  List<WebElement> rows = driver.findElements(By.xpath("//*[@class='gssb_m']"));  // Finding all elements under tag td for auto suggestions
  for (int i=0;i<rows.size();i++)  
  {  
   List<WebElement> cols = rows.get(i).findElements(By.tagName("span")); // span tag contains the result   
   System.out.println(cols.size());  
   for (int j=0;j<cols.size();j++)  
   {  
   System.out.println(cols.get(j).getText()); // printing them one by one 
   }  
  }  
  }  
 }  

No comments:

Post a Comment