0
package calendar_autosuggestivedropdown;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Cleartrip {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "F:\\Selenium\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.cleartrip.com/");
        Thread.sleep(2000L);
        driver.findElement(By.id("FromTag")).sendKeys("mum");
        // Thread.sleep(5000);
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[@class='autoComplete']/li")));
        driver.findElement(By.id("FromTag")).sendKeys(Keys.ENTER);

        String script = "return document.getElementById(\"FromTag\").value;";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        String scripttext = (String) js.executeScript(script);
        System.out.println(scripttext);  // Mumbai 

        // To City 

        driver.findElement(By.xpath("//input[@id='ToTag']")).sendKeys("Pari");
        String scriptTo = "return document.getElementById(\"ToTag\").value;";
        WebDriverWait waitTo = new WebDriverWait(driver, 5);
        waitTo.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[@id='ui-id-2']")));

        while (!scriptTo.equalsIgnoreCase("Paris, FR - Le Bourget (LBG)")) {

            driver.findElement(By.xpath("//input[@id='ToTag']")).sendKeys(Keys.DOWN);
            scriptTo = (String)js.executeScript(scriptTo);
            System.out.println(scriptTo); 
        }


    }

}
  • What is the exact line where the exception occurs? – João Farias Mar 3 '19 at 10:20
  • Line 56 : scriptTo = (String)js.executeScript(scriptTo); Exception : Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Pari is not defined – Palak Soni Mar 3 '19 at 10:42
0

As per my understanding, you are trying to select a particular matching option from that auto suggested drop down.

You can able to select Mumbai from the first drop down because there is only one match is coming but you are not able to select the second because there are multiple matches.

To select a desired matching option, I recommend you to use driver.findElements() instead of JavaScriptExecutor and need to modify locators in your code to make it work properly.

Below is the modified code which selects Paris, FR - Le Bourget (LBG) option from to auto suggestion drop down :

package calendar_autosuggestivedropdown;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Cleartrip {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "F:\\Selenium\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.cleartrip.com/");

        WebDriverWait wait = new WebDriverWait(driver, 15);

        // From
        WebElement from = driver.findElement(By.id("FromTag"));
        from.sendKeys("mum");
        // Locating options from 'from' dropdown
        String fromOptiosXapth = "//ul[@id='ui-id-1']/li";
        // Waiting until the from drop down options get displayed
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(fromOptiosXapth)));
        // Selecting the first option
        from.sendKeys(Keys.ENTER);
        // Fetching the value
        String fromPlace = from.getAttribute("value");
        System.out.println("=> From : "+fromPlace);

        // To
        WebElement to = driver.findElement(By.id("ToTag"));
        to.sendKeys("Pari");
        // Locating options from 'from' drop down
        String toOptionsXPath = "//ul[@id='ui-id-2']/li";
        // Waiting until the to drop down options get displayed
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(toOptionsXPath)));
        // Option that you want to select
        String optionThatYouWantToSelect = "Paris, FR - Le Bourget (LBG)";
        // Fetching all the to drop down options and storing in the list
        List<WebElement> toOptions = driver.findElements(By.xpath(toOptionsXPath));
        // Looping and selecting if it matches with our required option
        for(WebElement toOption: toOptions) {
            if(toOption.getText().trim().equalsIgnoreCase(optionThatYouWantToSelect)) {
                toOption.click();
                break;
            }
        }
        // Fetching the selected option from to drop down
        String toPlace = to.getAttribute("value");
        System.out.println("=> To   : "+toPlace);
    }
}

I hope it helps...

| improve this answer | |
  • Hi @Palak Soni, check my answer and let me know it is working for you or not? Still if you want to use JavaScriptExecutor then I will post an answer according to that as well... Thank you – Ali CSE Mar 3 '19 at 11:22
  • While running the program most of the time throwing TimeOut Exception what is the cause of it even i put wait for 10 Seconds. – Palak Soni Mar 4 '19 at 15:51
  • At what line are you getting this exception? I think, sometimes auto suggestion drop down taking time to load the list so you are getting that exception. Try to increase the wait time to 1 minute, it may solve the problem... – Ali CSE Mar 5 '19 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.