Thursday 9 February 2017

SELENIUM: How to Handle Auto-suggestions / How to get/read all Auto-suggestions options



How to Handle Auto-suggestions / How to get/read  all Auto-suggestions options:


We can handle Auto-suggestions by using Xpaths.
Example:
package com.rameshsoft.rameshselenium;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class AutosuggestionsDemo {
          @Test
          public void demo() throws InterruptedException, IOException
          {
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

                   WebDriver driver=new FirefoxDriver();
                   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                   driver.manage().deleteAllCookies();
                   driver.get("https://www.google.com");
                   Thread.sleep(4000);
                   driver.findElement(By.name("q")).sendKeys("selenium interview questions");
                   //getting all the Auto-suggestions in the List
                   List<WebElement> suggestions=driver.findElements(By.xpath(".//*[@id='sbtc']/div[2]/div[2]/div[1]/div/ul"));
                   //Iterating and printing the Text of each Auto-suggestion
                   for(WebElement ele:suggestions)
                   {
                             System.out.println(ele.getText());
                   }

}
}

No comments:

Post a Comment