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

StackOverflow https://stackoverflow.com/questions/17741060

Frage

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.

War es hilfreich?

Lösung

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();
}

Andere Tipps

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.

// allow autopuplation value to fill the text box.

// wait for 6 sec make sure auto value is inserted thread.sleep(6000L);

// clear auto filled value driver.findElement(By.name("txtBox")).clear();

driver.findElement(By.name("txtBox")).sendKeys("value");

Try first clicking on the Input textbox. This will trigger the auto populating dropdown box and then enter the required value using sendKeys

Either you can use tab or enter to Escape the scenario or,it is not possible by selenium if it is mandatory fields. (sad)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top