简体   繁体   中英

Selenium Handling dropDown with List if Select class not working

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

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, eg 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, eg "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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM