-1
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AutoSuggestions {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "F:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        Actions act = new Actions(driver);
        driver.get("https://www.homecentre.in/");
        WebDriverWait wait = new WebDriverWait(driver,30);


        driver.manage().window().maximize();
        driver.findElement(By.xpath("//*[@id=\"js-site-search-input\"]")).sendKeys("furniture");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<WebElement> allOptions = driver.findElements(By.xpath("/html/body/div[1]/header/div/div/div[1]/div[2]"));

        try {
            for (int i = 0; i < allOptions.size(); i++) {
                String option =  allOptions.get(i).getText();

                System.out.println(option);
                allOptions.get(i).click();
            }
        } catch (org.openqa.selenium.StaleElementReferenceException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }

            }
}
4
  • 1
    Why do you want to click them one-by-one? Normally when you pick an item from suggested list the list collapses so you are not able to click another one. All items from the list would be likely destroyed so you'll need to type something to invoke the list again. But that will be a new list so you won't be able to reuse saved WebElements
    – Alexey R.
    Aug 1, 2018 at 14:21
  • Then how would I know whether all the displayed links are working fine or not?
    – vikram
    Aug 2, 2018 at 11:01
  • 2
    Well, normally the items in search auto-suggestion are not the links but text that is put to the searchbox on-click. You should not check if all items work. You can just apply equivalence partitioning method to decrease the work load of your testing.
    – Alexey R.
    Aug 2, 2018 at 11:08
  • you should change your title to be more like a title and less like the question, put some of the title stuff into the question content before the code. Also, please tell us what your code is doing and what issue you are encountering.
    – Malachi
    Aug 2, 2018 at 17:30

1 Answer 1

0

It depends on how the system behaves when you are trying to select the second, third... elements.

The general idea is to the search again.

I can imagine the following to situations:

1 - The input get blank when you select an item. 2 - The searched text stays when you select an item.

On (1), you would have to re-do the procedure of clicking in the input, filling it and waiting, before clicking. On (2), you would have to just click and wait.

Either way, this would be done inside the loop into which you go through the many input values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.