1

I am trying to automate a testcase where textbox provides intelligence to autocomplete the field. Please find the link below for autocomplete text box : http://demoqa.com/autocomplete/

Please find the code written by me

  dr.findElement(By.id("tagss")).sendKeys("a");
  Thread.sleep(300);
//  dr.findElement(By.id("ui-id-53")).click();
  Actions act = new Actions(dr);
  act.moveToElement(dr.findElement(By.id("ui-id-53"))).click().build().perform();

This code is unable to find and locate options in intelligence provided by browser. Please help.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66

2 Answers2

1

You are not able to locate the auto-suggested option element in the DOM because HTML id for those options changes after page reloads.

In this case, you need to use XPath to identify the element. Suppose you want to click on Java auto-suggested option, then your code should be -

System.setProperty("webdriver.chrome.driver","C:\\WebDriver\\TestAutomation\\grid\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demoqa.com/autocomplete/");
driver.manage().window().maximize();
driver.findElement(By.id("tagss")).sendKeys("a");
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ui-id-1"))));
WebElement javaOption = driver.findElement(By.xpath(".//li[@class='ui-menu-item' and text()='Java']"));
javaOption.click();

Its not good practice to use Thread.sleep();

hope this help you.

Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
  • I tried but my reputation is 11 and it says my reputation is not sufficient to do so. Sorry about that amol –  Dec 28 '17 at 07:09
0

Because you are using dynamic id which may change at runtime. Try this code and let me know for any queries -

public class AutoSuggest {

     public static void main(String[] args) throws InterruptedException {
            try {

                  System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe");
                  WebDriver driver = new ChromeDriver();
                  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                  WebDriverWait wait=new WebDriverWait(driver,50 );

                  driver.manage().window().maximize();

                  driver.get("http://demoqa.com/autocomplete/");
                  driver.findElement(By.id("tagss")).sendKeys("a");
                  Actions act = new Actions(driver);
                  List<WebElement> lst= driver.findElements(By.xpath("//li[contains(@id,'ui-id')]"));
                  for(WebElement element:lst){
                      element.click();
                      break;
                  }

            } catch (Exception e) {
                e.printStackTrace();
            }
     }


}
Indrapal Singh
  • 364
  • 1
  • 2
  • 13
  • Thanks for your help. Its working. I changed the code little bit. ` List option = dr.findElements(By.className("ui-menu-item")); for(WebElement x : option) { if(x.getText().toLowerCase().equals("java")) { x.click(); } }` –  Dec 28 '17 at 06:33