Friday 15 May 2015

Working with google auto suggest and select an option from the auto suggested links.

package Assignments;

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 CapturingAllAutoSuggested {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
String exp = "wipro wiki";

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

List<WebElement> lst = driver.findElements(By.xpath("//div[div[div[div[div[input[@id='lst-ib']]]]]]/following-sibling::div/div/div/div/ul/li"));

for(WebElement option : lst)
{
String s = option.getText();
System.out.println(s);

if(s.equals(exp))
{
System.out.println("Trying to select: "+exp);
                        option.click();
                        break;
}


}


}

}

1 comment:

  1. Sai how you wrote xpath for second driver.findelement??

    ReplyDelete