I am trying to automate auto complete suggestions on amazon.com. But unlike google search options, xpath of suggestions is always changing. The code I posted doesn't work every time because sometimes the xpath/id/cssselector of the desired suggestion is changing (@id=\"issDiv8\"] sometimes it is "issDiv4" or "issDiv6" and so on.
WebElement searchTextField = driver.findElement(By.id("twotabsearchtextbox"));
searchTextField.sendKeys("turbo");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id=\"issDiv8\"]")));
List<WebElement> autoSuggest = driver.findElements(By.xpath("//*[@id=\"issDiv8\"]"));
System.out.println("Auto Suggest List ::" + autoSuggest.size());
for (int i = 0; i < autoSuggest.size(); i++) {
System.out.println(autoSuggest.get(i).getText());
if (autoSuggest.get(i).getText().equals("turbotax")) {
autoSuggest.get(i).click();
System.out.println("Success");
break;
Use WebdriverWait
to handle dynamic element and use the following xpath
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@data-keyword='turbotax']")));
element.click()