Ravi Anand
Ravi Anand

Reputation: 47

how to handle Auto suggestion in selenium

//for source
 WebElement from= driver.findElement(By.id("autocomplete_source"));
      from.clear();
      from.sendKeys(FromCity);
      Thread.sleep(3000);
      Actions builder=new Actions(driver);
      builder.moveToElement( from.findElement(By.xpath("//*[@class='acResults']/ul/li[1]/span"))).click().build().perform();

//for destination

 WebElement to=driver.findElement(By.id("autocomplete_dest"));
      to.clear();
      driver.findElement(By.id("autocomplete_dest")).sendKeys(ToCity);
builder.sendKeys(Keys.ARROW_DOWN).click().build().perform();
      WebDriverWait wait=new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*        [@class='acResults']/ul/li/span")));
     to=driver.findElement(By.cssSelector(".acResults>ul>li>span"));
      Thread.sleep(2000);
          to.click();

/* there are two text box source and destination,source is getting handled but destination is not working. when i am passing Chennai in destination list is showing arrow down is working but it is not selecting and giving error locator is not visible */

Upvotes: 1

Views: 41742

Answers (3)

Mashekul Alam
Mashekul Alam

Reputation: 11

Please use the code. it should work for Autocomplete for address forms.

import java.util.List;

public class AutoSuggest {

@Test(description="Auto Suggest")
public void selectValues() throws InterruptedException 
{
/*  System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    FirefoxDriver driver = new FirefoxDriver();
    */

System.setProperty("webdriver.chrome.driver","C:\Users\abcd\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    //driver.get("http://www.google.com");


    driver.get("https://www.google.com/maps/search/maps+google/@37.5979349,-77.504561,15z/data=!3m1!4b1");
    driver.manage().window().maximize();
    driver.findElement(By.id("searchbox")).sendKeys("Glen Allen");
    Thread.sleep(2000);

    //cocRegion
Actions act = new Actions(driver);
    act.sendKeys(Keys.DOWN).perform();
    act.sendKeys(Keys.ENTER).perform();
    // This code for open a new Tab
    //act.keyDown(Keys.CONTROL).sendKeys("t").build().perform();

    /**
     * Example for Visibility of Elements located by
     */
    /*WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("locSelectVal")));

    List<WebElement> list = driver.findElements(By.id("locSelectVal"));

    System.out.println("Auto Suggest List ::" + list.size());

    for(int i = 1 ;i< list.size();i++)
    {
        System.out.println(list.get(i).getText());

        if(list.get(i).getText().equals("Glen Allen"))
        {
            list.get(i).click();
            break;


        }*/
    //driver.close();
    }



}

 import java.util.List;
 








import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
 
public class AutoSuggest {
 


	@Test(description="Auto Suggest")
	public void selectValues() throws InterruptedException 
	{
	/*	System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		FirefoxDriver driver = new FirefoxDriver();
		*/
System.setProperty("webdriver.chrome.driver","C:\\Users\\abcd\\chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		//driver.get("http://www.google.com");
		
		
		driver.get("https://www.google.com/maps/@37.5969128,-77.5061491,16.75z");
		driver.manage().window().maximize();
		driver.findElement(By.id("cocRegion")).sendKeys("Glen Allen");
		Thread.sleep(2000);
	
		//cocRegion
	Actions act = new Actions(driver);
		act.sendKeys(Keys.DOWN).perform();
		act.sendKeys(Keys.ENTER).perform();
		// This code for open a new Tab
		//act.keyDown(Keys.CONTROL).sendKeys("t").build().perform();
	
		/**
		 * Example for Visibility of Elements located by
		 */
		/*WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("locSelectVal")));
		
		List<WebElement> list = driver.findElements(By.id("locSelectVal"));
		
		System.out.println("Auto Suggest List ::" + list.size());
		
		for(int i = 1 ;i< list.size();i++)
		{
			System.out.println(list.get(i).getText());
			
			if(list.get(i).getText().equals("Glen Allen"))
			{
				list.get(i).click();
				break;
			
			
			}*/
		//driver.close();
		}
	

		
	}
	

enter image description here

Upvotes: 1

Sitam Jana
Sitam Jana

Reputation: 3129

Tried with the following code. Worked for me:

WebDriver driver = new FirefoxDriver();
driver.get("http://in.via.com/bus-tickets");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.findElement(By.xpath("//input[@id='autocomplete_source']"))
    .clear();

driver.findElement(By.xpath("//input[@id='autocomplete_source']"))
    .sendKeys("Chen");

driver.findElement(
    By.xpath("//div[@class='acResults'][1]//li[1]//span")).click();

driver.findElement(By.xpath("//input[@id='autocomplete_dest']"))
    .clear();

driver.findElement(By.xpath("//input[@id='autocomplete_dest']"))
    .sendKeys("Co");

driver.findElement(
    By.xpath("//div[@class='acResults'][2]//li[1]//span")).click();

Upvotes: 0

Deepak
Deepak

Reputation: 336

provide HTML. I tried auto suggestion on redbus.in & is working without using Action class. Here is a code which works for me on FF/Chrome-

driver.get("http://www.redbus.in/");
driver.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("pune");
driver.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("Chennai");

Please provide url or html so we can dig more in to it.

Below code is working for me on FF(http://in.via.com/bus-tickets)-

driver.get("http://in.via.com/bus-tickets/");
WebElement from = driver.findElement(By.xpath(".//*[@id='autocomplete_source']"));
WebElement to =driver.findElement(By.xpath(".//*[@id='autocomplete_dest']"));
from.clear();
from.sendKeys("Pune");
to.clear();
to.sendKeys("Chennai");
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div[9]/ul/li")));
driver.findElement(By.xpath("html/body/div[9]/ul/li")).click();

//clicking submit

driver.findElement(By.xpath(".//*[@id='bookingDiv']/div[21]/input")).click();

Upvotes: 2

Related Questions