0

Looking for the list with what user has typed Trying to automate the City Selection but unable to get the Origin City List when I type some matching letters. Rather I am getting the default list displayed in the dropdown

Code trials:

driver.get("https://www.makemytrip.com");
driver.findElement(By.xpath("//input[@id='fromCity']")).click();
String TypeSrc="R";
String sourceCity="Ranchi";
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement el= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='text' and @role='combobox']")));
el.sendKeys(TypeSrc);   
List<WebElement> li=driver.findElements(By.xpath("//ul[@role='listbox']//li//div//p"));
for(int i=0;i<li.size();i++){           
{
    System.out.println(li.get(i).getText());
}

Actual output I am getting:

Mumbai, India
Chhatrapati Shivaji International Airport
Delhi, India
Delhi Airport
Bangkok, Thailand
Suvarnabhumi Airport
Bangalore, India
Bengaluru International Airport
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    you'll need to wait or sleep there... findElements will return if at least 1 element is found... meanwhile the client-side script may be populating the list. Either use a WebDriverWait (and maybe also catch stale element exception and re-run..) or use a set amount of time to wait for the list to populate. – pcalkins Aug 07 '19 at 20:35

2 Answers2

0

When you type the desired outbound city name the list is being populated via AJAX call which has asynchronous nature and it not immediate.

WebDriver is too fast hence you're catching the default list of cities (most popular ones) therefore I would recommend introducing another WebDriverWait call in order to wait until "SUGGESTIONS" text is visible:

enter image description here

You can use XPath Selector assuming starts-with() function like:

WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='text' and @role='combobox']")));
el.sendKeys(TypeSrc);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[starts-with(text(),'SUGGESTIONS')]")));
//do what you need here
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

To extract the list with respect to the text which user have typed with in the From field of City Selection as the element is a dynamic element you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java 8's stream() and map() along with the following Locator Strategy:

  • Code Block:

    public class A_demo 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            WebDriver driver = new ChromeDriver(options);
            driver.get("https://www.makemytrip.com");
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class, 'fsw_inputField') and @id='fromCity']"))).sendKeys("R");
            new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='react-autosuggest__suggestions-list']//li//p[contains(@class, 'blackText')]")));
            List<String> cities = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='react-autosuggest__suggestions-list']//li//p[contains(@class, 'blackText')]"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
            System.out.println(cities); 
        }
    }
    
  • Console Output:

    [Rajkot, India, Raipur, India, Rajahmundry, India, Rio De Janeiro All Airports, Brazil, Rio De Janeiro, Brazil, Rio de Janeiro, Brazil, Rome, Italy, Rome, Italy, Rome, Italy, Riga, Latvia, Rodhos, Greece, Recife, Brazil, Rennell, Solomon Islands, Rengat, Indonesia, St Denis, Reunion, Rockland, United States, Rotorua, New Zealand, Rockhampton, Australia, Durgapur, India]
    
  • Browser Snapshot:

makemytrip_from

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352