How to "click" on certain proposal from autosuggestion on Amazon using Selenium?

Alex :

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.

enter image description here

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;
KunduK :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156643&siteId=1