4

I need to enter some text in a autocomplete textbox. Then I will select a option from that autocomplete option and need to click it.

I have tried with the following code:

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    String textToSelect = "headlines today";

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/");
    Thread.sleep(2000);
    WebElement autoOptions= driver.findElement(By.id("lst-ib"));
    autoOptions.sendKeys("he");

    List<WebElement> optionsToSelect = driver.findElements(By.tagName("li"));

    for(WebElement option : optionsToSelect){
        System.out.println(option);
        if(option.getText().equals(textToSelect)) {
            System.out.println("Trying to select: "+textToSelect);
            option.click();
            break;
        }
    }
6
  • What is the problem with your code? Commented Jun 16, 2015 at 9:38
  • what error you are getting? Commented Jun 16, 2015 at 9:40
  • @VikasNehaOjha In my code the for loop iteration is not running Commented Jun 16, 2015 at 11:48
  • @HelpingHands It is not showing any error! But, the program is keep on running. In optionsToSelect it is getting the list of values. I'm trying to iterate it in for loop and comparing the values. Commented Jun 16, 2015 at 11:51
  • Do a sleep or an explicit wait before the List<WebElement> optionsToSelect. Also try to print the list before the for loop so you can see the number of elements in the list. Commented Jun 16, 2015 at 11:51

4 Answers 4

1
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");

List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='sbqs_c']"));

for(WebElement option : optionsToSelect){
    System.out.println(option);
    if(option.getText().equals(textToSelect)) {
        System.out.println("Trying to select: "+textToSelect);
        option.click();
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can do like this i have used google home page auto suggest as an example

public class AutoSelection {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.google.com");

        driver.findElement(By.name("q")).sendKeys("mahatama gandhi");
        List<WebElement> autoSuggest = driver.findElements(By
            .xpath("//div[@class='sbqs_c']"));
        // verify the size of the list
        System.out
            .println("Size of the AutoSuggets is = " + autoSuggest.size());
        // print the auto suggest
        for (WebElement a : autoSuggest)
            System.out.println("Values are = " + a.getText());
        // suppose now you want to click on 3rd auto suggest then simply do like
        // this
        autoSuggest.get(2).click();
    }
}

2 Comments

Thanks for your reply rajnish. But in my case I need to compare the exact string/text in that suggestion. Instead of giving as index values inside get().
But any how I found the error that I used the tag name instead of using xpath. Now it works. Thanks a lot
0
driverName.findElement(By.xpath("XPATH Location")).sendKeys("KeyNameYouWantToSearch" , Keys.TAB);

Comments

0

We can use java 8 stream API to filter and collect Web elements in an Array. Clicking will be easier using index.

// Collect 5 autocomplete entries lists

List<WebElement> list = driver.findElements(By.cssSelector("#ui-id-1 
li:nth-child(n)")).stream()
            .limit(5)
            .collect(Collectors.toList());

// This will click the first element from an autocompleted list

list.get(0).click();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.