0

I was trying to handle dropdown of https://www.airvistara.com/trip/ source but I am facing a weird issue in the below code snippet:

driver.get("https://www.airvistara.com/trip/");    
driver.manage().window().maximize();    
driver.findElement(By.className("location_icon")).click();    
List<WebElement> elements=driver.findElements(By.tagName("div"));

for(int i=0;i<elements.size();i++){
    if(elements.get(i).getAttribute("class").contains("scombobox-list"))
    {
        System.out.println(elements.get(i).getText());
        elements.get(i).click();
        break;
    }
}

It works fine and selects an item if I write elements.get(i).click(); But if I put the value of i as any number(less than elements.size), then it doesn't select any city.

If i write elements.get(4).click(); is not selecting any value

2 Answers 2

1

The problem is that the DIV with the class scombobox-list is not an element in the dropdown, it IS the dropdown. There are more then one on the page, e.g. Origin, Destination, etc. Your code elements.get(4).click(); attempts to click the 4th dropdown rather than the 4th option in the dropdown. You can get around this by looking further up the DOM from the dropdown you want and find a unique element specific to the Origin, Destination, etc. In this case there is a DIV that is the container for all of the Origin related elements,

<div class="col-md-3 col-sm-6 widget-div-input scombobox" id="departsfrom-div" placeholder="Origin">

Since it has an ID, we can use that in our locators to specify children elements without having to worry about other possible matches. This will solve the problem of multiple existing dropdowns.

Much of your code is looping and doing string matches to find the element you want. You would be much better served to achieve this with locators. In this case, you can click the dropdown to open it and then click on the element that contains the airport name you want, e.g. "Ahmedabad (AMD)"

driver.get("https://www.airvistara.com/trip/");    
driver.manage().window().maximize();    
setOriginAirport("Ahmedabad (AMD)");

and then have a function that sets the origin airport given the airport name.

public void setOriginAirport(string airportName)
{
    driver.findElement(By.cssSelector("#departsfrom-div .location_icon")).click();
    By locator = By.xpath("//div[@id='departsfrom-div']//span[contains(.,'" + airportName + "')]");
    WebElement e = new WebDriverWait(driver, 5).Until(ExpectedConditions.elementToBeClickable(locator));
    Thread.sleep(500); // may need this even after wait
    e.click();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for suggesting the workaround with locators :) But still it gets failed on setOriginAirport("Chandigarh (IXC)"); The error is element not visible even if you try with any airportName text.
Sorry... I wasn't able to test it earlier. I've updated the code and it should work now. I was clicking on the wrong thing and the dropdown wasn't opening before. That's why the element was not visible. I've added a brief sleep() after the wait. Generally it's not a good practice to use sleeps but without it, it wasn't able to click the element properly. You should try it on your machine without it and see if it works or not.
0

Instead of your code you can try the below code:

driver.get("https://www.airvistara.com/trip/");    
driver.manage().window().maximize();    
driver.findElement(By.className("location_icon")).click();    
WebElement wbelement=driver.findElement(By.className("scombobox-list"));
List<WebElement> elements = wbelement.findElements(By.className("scombobox-mainspan"));
for(int i=0;i<elements.size();i++){
System.out.println(elements.get(i).getText());
elements.get(i).click();
break;
}

if you want to click any specific location, you can include if condition in for loop

1 Comment

Its still not working for elements.get(4).click(); Its really weird since the loop for i has an int value then also elements.get(i).click(); works but elements.get(4).click(); doesnot work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.