Search code examples
javaseleniumselenium-webdriverjunitbrowser-automation

How to get the text from the auto suggestion of the auto complete text box?


My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.

So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.

So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).

My code:

driver.findElement("//input[@id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[@id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[@id='phSearchInput']").SendKeys(KEYS.ENTER);

Here the above code is only working for my correct option is shows as the second option of the auto-suggested texts.

So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.

Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.

Thanks

enter image description here


Solution

  • In that case you can use the findelements functionality. So you can say:

    List<WebElement> elements = driver.findElements(by.xpath("//a[@class='autoCompleteRowLin‌​k']"); 
    

    And then for each list item you can use getText to get the exact text. Then you can assert it with the expected values