5

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.

Buras
  • 3,069
  • 28
  • 79
  • 126
  • Have to you tried other browsers??.Also why are you waiting for 0 seconds in the web driver wait. – Madusudanan Jul 19 '13 at 09:06
  • Thank You for reply. 1) explicit wait has no issues, i can use `(driver, 120)`. The page loads fine. The issue occurs when "Associated Press, The" gets into the input box ... Manually - no problem, but as it goes there automatically - it simplly wouldn't load the options. 2) I also tried FireFox - no improvements. – Buras Jul 19 '13 at 09:16
  • 1
    The title was very promising but I don't quite see how the accepted answer addresses the fact that sendKeys() doesn't make the autocomplete dropdown list appear the way it would when then same text input was typed in manually. And why didn't you give a real URL where one could inspect the problematic element? – Cpt. Senkfuss Jan 27 '16 at 09:57

5 Answers5

1

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();
}
joostschouten
  • 3,863
  • 1
  • 18
  • 31
1

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.

0

// 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");

nikil
  • 1
0

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

  • Could you give an example of how to do that? – Adam Apr 12 '17 at 14:54
  • driver.findElement(By.name("fromLocation")).click(); driver.findElement(By.name("fromLocation")).sendKeys("XYZABC"); driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB); – Ritesh Kadam Apr 24 '17 at 15:10
0

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