1

I am entering the name which is I want to search then it is displayed auto suggestion, but I am not able to select the first option from autosuggestion.

This is eclipse oxygen with TestNG plugin

driver.findElement(By.className("searchfilter")).sendKeys("Abilify");// This is working But after that option selection is not working

driver.findElement(By.cssSelector(".list-group-item:first-child")).click(); // Issue is here

Html code:

<li class="list-group-item list-group-item-action py-3 tabindex fs-1-1 bg-offwhite" id="indexTab1" href="970-ABILIFY" name="ABILIFY - ARIPIPRAZOLE">ABILIFY - ARIPIPRAZOLE</li>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
krina
  • 27
  • 5

3 Answers3

0

1- First enter your word in the search box.

2- then wait until the Elements of your search are visible or clickable.

 WebDriverWait wait = new WebDriverWait(driver, milliseconds);
 wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[@id='indexTab1']"))));

3- then click on the desired element

driver.findElement(By.xpath("//li[@id='indexTab1']")).click();
0xM4x
  • 460
  • 1
  • 8
  • 19
0

I think, yours is single page application. Most probably its angular js app. These SPAs keeps updating/displaying options in the dropdown based on what you type in the dropdown editbox.

Please try the following code,

driver.findElement(By.className("searchfilter")).sendKeys("Abilify");

driver.findElement(By.Xpath("//li[contains(text(), 'Abilify')]")) . click() ; // Note : Selenium always work on first element if there are mutiple matches.

You can also parameterize your selection value like the following

String temp=" Abilify"; driver.findElement(By.className("searchfilter")).sendKeys(temp);

driver.findElement(By.Xpath("//li[contains(text()," + temp +")]")).click() ;

Regards, Partha

Partha
  • 107
  • 9
-1

Once you invoke sendKeys() with the desired text, to select the first auto suggestion you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.findElement(By.className("searchfilter")).sendKeys("Abilify");
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.list-group-item-action[name='ABILIFY - ARIPIPRAZOLE'][href$='-ABILIFY']"))).click();
    
  • xpath:

    driver.findElement(By.className("searchfilter")).sendKeys("Abilify");
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(@class, 'list-group-item-action') and @name='ABILIFY - ARIPIPRAZOLE'][contains(., 'ABILIFY - ARIPIPRAZOLE')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352