0

I am trying to verify if the selected value in an AutoSuggest Dropdown is correct using Selenium. But using the getText() doesn't return any results.

Below is my code

public class AutoSuggestDropdownsTest {

public static void main(String[]args) throws InterruptedException {
   System.setProperty("webdriver.chrome.driver", "/resources/executables/chromedriver") ; 
    WebDriver driver = new ChromeDriver();
    driver.manage().deleteAllCookies();
    driver.get("https://example.com/");
    driver.findElement(By.xpath("//*[@class='stamp remove-sign']")).click();
    driver.findElement(By.xpath("//input[@id='or-search']")).sendKeys("to");
    driver.findElement(By.xpath("//input[@id='or-search']")).sendKeys(Keys.DOWN);
    driver.findElement(By.xpath("//input[@id='or-search']")).sendKeys(Keys.DOWN);
    driver.findElement(By.xpath("//input[@id='or-search']")).sendKeys(Keys.DOWN);
    System.out.println(driver.findElement("//input[@id='or-search']")).getText());
    System.out.println("hello");

    driver.quit();


}
SUPARNA SOMAN
  • 2,311
  • 3
  • 19
  • 35
  • Can you print t and let us know whats the output you see. – Vin Feb 11 '20 at 19:45
  • It doesn't print anything with getText. I just updated the code above. So the output with the above code is : hello – SUPARNA SOMAN Feb 11 '20 at 19:48
  • Everything else works fine until other than the getText() – SUPARNA SOMAN Feb 11 '20 at 19:49
  • I tried the same testcase with google.com when the list is autopopulated when you type something in that field. I assume your site is also similar to googles search field. The problem is that the text that is highlighted when you press down arrow is not added back to the input html tag. You need to find the actual xpath to that autopopulate. – Vin Feb 11 '20 at 20:24
  • Try the following code in your script then you can understand what you need to look for. `driver.get("https://google.com") driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("to") driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(Keys.DOWN) driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(Keys.DOWN) driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(Keys.DOWN) String t= driver.findElement(By.xpath("//li[1]/div[contains(@class,'suggestions-inner-container')]//span")).getText() println(t)` – Vin Feb 11 '20 at 20:26

1 Answers1

0

Usually autosuggestion results take some time to appear, so if you do the sendKeys(Keys.DOWN) immediately after sending the to it is very likely that no autosuggestions appeared yet, and this way the DOWN key has no effect. To check if this is the case (for development purpose of the test only) put a sleep 10s, and check if with the sleep the text you think should appear, actually appears.

  • if it does appear, then you should implement some kind of smart waiting for the results to appear.
  • if it doesn't appear, maybe there is another problem, in which case it would be very useful if you could post the html of the page after the autossugestions appeared.
Jeni
  • 1,088
  • 12
  • 30