Thursday, January 16, 2014

Select Auto suggest on search field for a List item using WebDriver

The below code is for searching a text automatically from the auto suggest; mainly for a list item.

driver.get("http://www.indiabookstore.net");
driver.findElement(By.id("searchBox")).sendKeys("Alche");
 Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("//div[@class='acResults']//li"));

 // List <WebElement> listItems = driver.findElements(By.xpath("//div[contains(@class,'acResults')]//li"));   

//List <WebElement> listItems = driver.findElements(By.cssSelector(".acResults li"));

// List<WebElement> link = driver.findElement(By.id("Element")).findElements(By.tagName("li"));

 listItems.get(0).click();
 driver.findElement(By.id("searchButton")).click();

NOTE: get(0) is the first option displayed on searching keywords
           get(1) is the second option displayed on searching keywords.

1 comment:

  1. thanks - http://letusnotifyyou.com/print-selenium-autosuggestion-text/

    ReplyDelete