Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill in an autocomplete inputbox using Selenium? (Why an automated input does not load autocomplete options BUT a manual input does?)

The following code tests an autocomlete box of a webpage:

public class Test {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe");     
        WebDriver driver = new ChromeDriver();
        driver.get("http://www..............com"); 
        driver.switchTo().frame("mainFrame");

        WebDriverWait waitst = new WebDriverWait(driver, 120);
        waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));

        WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
        WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small"));
        sourceTitle.sendKeys("Times"); 
        Thread.sleep(5000);
        Actions actions = new Actions(driver);
        actions.click(small).perform();

    }

}

Why doesn't the autosuggest box load? IMPORTANT: try to type in "..........." manually ... the autocomplete box will load perfectly fine!!! So, why does not cssSelector work, why doesn't it load the autocomplete box?

How come an automated input does not allow for autocomplete options BUT a manual input does???

PS: I also tried fireEvent, sendKeys but nothing works.

like image 490
Buras Avatar asked Nov 03 '22 18:11

Buras


2 Answers

I tried your code, it does exactly what the manual walkthough does. "Associated Press, The" returns only a "No Match, please try sources". In your code you then try to click on the next form list item, not the results popup. The autosuggest popup is dynamically populated at the top of your html page positioned under the input form. The following code does select the first option on your drop down.

@Test
public void test() throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.lexisnexis.com/hottopics/lnacademic/?verb=sf&sfi=AC00NBGenSrch"); 
        driver.switchTo().frame("mainFrame");

        WebDriverWait waitst = new WebDriverWait(driver, 0);
        waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));

        WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
        sourceTitle.sendKeys("Times"); 
        Thread.sleep(5000);
        WebElement firstItem = driver.findElement(By.xpath("//*[@class='auto_suggest']/*[@class='title_item']"));
        firstItem.click();
}
like image 80
joostschouten Avatar answered Nov 08 '22 11:11

joostschouten


I found a workaround about this. My problem was:

  1. Selenium inputted 'Mandaluyong' to an auto-suggest location field
  2. The auto-suggest field appears together with the matched option
  3. Then selenium left the auto-suggest drop-down open not selecting the matched option.

What I did was:

        driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
        driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);

This is because on a manual test, when I try to press TAB key, two things were done by the system:

  1. Picks the matched option from the auto-suggest drop-down
  2. Closes the auto-suggest drop-down

Hope this helps.

like image 41
TesterFromA3rdWorldCountry Avatar answered Nov 08 '22 11:11

TesterFromA3rdWorldCountry