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

爱⌒轻易说出口 提交于 2019-12-24 03:47:17

问题


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;

回答1:


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


来源:https://stackoverflow.com/questions/56274695/how-to-click-on-certain-proposal-from-autosuggestion-on-amazon-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!