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();

14 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 - http://letusnotifyyou.com/print-selenium-autosuggestion-text/

    ReplyDelete
  3. Thanks for using the tag names instead of the xpath in the example, this helped me in solving an automation issue related to suggestions.

    mobile app development companies

    ReplyDelete
  4. I like your post.. Great information.Thank you...
    Mobile game development company

    ReplyDelete
  5. How to print the first paragraph from the results?

    ReplyDelete

  6. if(times.equalsIgnoreCase(time)) {
    String xp3 ="//ul[@class='dropdown-content select-dropdown']/li["+i+"]/span"; //syntex
    System.out.println(xp3);
    WebElement txt = driver.findElement(By.xpath(xp3));
    System.out.println(txt.getAttribute("innerHTML"));
    txt.click();
    System.out.println("click done");
    break;
    }

    I'm not able to click particular value of dropdown. please suggest me and I tried with index and text also but not

    ReplyDelete
  7. Thank You! For sharing such a great article, It’s been an amazing article. It provides lot’s of information, I really enjoyed to read this, I hope, I will get these kinds of information on a regular basis from your side.
    Apps Development Company in South Africa

    ReplyDelete
  8. Hi, I Read your blog and I feel it is a very wonderful, informative blog .
    There are a lot of good information on this blog, I'd like to Share and I think people will get a lot of support from this blog.
    Thank you for sharing this informative blog, please keep up and share some unique posts with us in the future
    Software testing company in India
    ARVR Development Company in India
    Technical Support Company in India

    ReplyDelete
  9. Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up virtual reality development services in India

    ReplyDelete