Thursday, 16 May 2013

Locate and select Auto suggest on search field for a List item (/li) 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("/html/body/div[4]/ul/li"));
listItems.get(0).click();
driver.findElement(By.id("searchButton")).click();
    
Note:  We can also repalce the xpath locator,
By.xpath("/html/body/div[4]/ul/li") with By.xpath("//div[4]/ul/li")

It's not a better way to use the above xpath locator; meanwhile these locators can be replaced with one of the following options (use csslocators for better solution).
List <WebElement> listItems = driver.findElements(By.xpath("//div[contains(@class,'acResults')]//li"));    
List <WebElement> listItems = driver.findElements(By.xpath("//div[@class='acResults']//li"));
List <WebElement> listItems = driver.findElements(By.cssSelector(".acResults li"));
List<WebElement> link = driver.findElement(By.id("Element")).findElements(By.tagName("li"));

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















Anchor Tag :

HTML

<head>
<body id="data-search" class="hassidebar">
<ul id="material-result-list" style="top: 183px; left: 396.5px; width: 270px; display: block;">
<li>
<a>nitrate/0.2</a>
</li>
</ul>

CODE

Here, we need to click on specific anchor tag.

List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("a")); 
listItems.get(2).click();

7 comments:

  1. A few improvements to your example:

    - don't use sleep. If I was manually testing a web site I would not wait for 3 seconds so my automation should not either. I want until the list appears. My automation will wait until the list appears.

    My full solution would be:

    @Test
    public void autoCompleteTest() {
    String resultsLocator = "div.acResults";
    String resultFourLocator = resultsLocator + ">ul>li:nth-of-type(4)";
    driver.get("http://www.indiabookstore.net/");
    driver.findElement(By.cssSelector("#searchBox")).sendKeys("Alche");
    WebElement results = driver.findElement(By.cssSelector(resultsLocator));
    waitForIsDisplayed(results);
    WebElement result = driver.findElement(By.cssSelector(resultFourLocator));
    System.out.println(result.getText());
    result.click();
    driver.findElement(By.cssSelector("#searchButton")).click();
    }

    private void waitForIsDisplayed(WebElement we) {
    do {
    try { Thread.sleep(50); } catch (InterruptedException e) { }
    } while(!we.isDisplayed());
    }

    ReplyDelete
    Replies
    1. Hi Darrell!

      I agree with your point in this.
      Instead of waitForIsDisplayed, I use expectedcondition which is similar to your script.

      I specifically mentioned in this post:
      http://seleniumworks.blogspot.in/2013/05/iframes-finding-presenceofelementlocate.html

      Lemme know your thoughts..

      -Sams

      Delete
    2. Hi Prashanth Can u please share the code here

      Delete
  2. Thanks for the great information in your blog Selenium Training in Chennai

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

    ReplyDelete