Rathna
Rathna

Reputation: 25

Select an option from the first of two drop-downs with same class name

I have selected the lowest fare and clicked on view seats and am trying to select an option from the first drop-down(boarding point), but am not able to do it as both of them have the same class name. Help is appreciated.

The code which I have used is:

package week3.redbus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

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

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.redbus.in/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        //page1
        driver.findElement(By.id("txtSource")).sendKeys("Hyderabad");
        driver.findElement(By.id("txtDestination")).sendKeys("Chennai");
        driver.findElement(By.id("txtOnwardCalendar")).click();
        driver.findElement(By.xpath("//div[@id = 'rbcal_txtOnwardCalendar']/table[1]/tbody/tr[4]/td[3]")).click();
        driver.findElement(By.xpath("//div/button[@id = 'searchBtn']")).click();


        List <WebElement> fares = driver.findElements(By.xpath("//div/span[@class = 'fareSpan']//span"));
        int fareNum = fares.size();     
        int[] fareInt = new int [fareNum];
        int index =0;
        System.out.println( "Num of fares is "+fareNum );
        for (WebElement e : fares) {
            String fareN = e.getText();
            fareInt[index] = Integer.parseInt(fareN);//convert string to integer array
            index++;

        }
        for (int i : fareInt) {
            System.out.println(i);

        }
        Arrays.sort(fareInt);//sort to ascending order
        System.out.println("Sorted fare list");

        for (int i : fareInt) {
            System.out.println(i);          
        }

        //the least price
        System.out.println("Lowest price is "+fareInt[0]);
        String x = Integer.toString(fareInt[0]);
        driver.findElement(By.xpath("//span[text() = '"+ x +"']/../../../button[@class = 'viewSeatsBtn']")).click();


        //if (driver.findElement(By.xpath("//select[@class ='select-apsrtc']/option[text() = '-- Boarding points --']")).isDisplayed()) {
            //driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select")).click();
        Thread.sleep(3000);
            Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
            bp.selectByIndex(6);
            driver.findElement(By.xpath("//button[@class = 'continueBtn']")).click();

        //} 


    }           
}

@AGill By doing the above code, I am getting the following exception:

Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (471.5, 105.80000305175781). Other element would receive the click: <div class="SortBar clearfix MB"></div>

@ dcsohl I have modified the last part of the code as you mentioned, but I am getting the same exception as above.

List<WebElement> dd = driver.findElements(By.className("select-apsrtc"));
        int ddN = dd.size();
        for (int i = 0; i < ddN; i++) {
            WebElement dd1 = dd.get(0);
            Select bp = new Select(dd1);

            //Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
            bp.selectByIndex(6);
            driver.findElement(By.xpath("//button[@class = 'continueBtn']")).click();
        }

@AGill, I modified the code as you suggested as follows:

Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
            Thread.sleep(3000);
            bp.selectByVisibleText("Ameerpet - 05:25 PM");
            driver.findElement(By.xpath("//button[@class = 'show-seatLayout']")).click();

But, I am getting the following exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate element with text: Ameerpet - 05:25 PM

Upvotes: 1

Views: 1317

Answers (2)

AGill
AGill

Reputation: 788

You can use the following locators (css selector) to uniquely identify the two drop downs. Locator for the Boarding point drop down will be:

[class='selectContainer BPContainer'] [class='select-apsrtc']

And locator for Dropping point drop down will be:

 [class='selectContainer DPContainer'] [class='select-apsrtc']

You can use xPath too if you want. You already have a correct xPath for boarding ponit. For dropping point use:

//div[@class = 'selectContainer DPContainer']/select[@class = 'select-apsrtc']

Then you can do something like:

Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
        bp.selectByIndex(6);

Select droppingPoint = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer DPContainer']/select[@class = 'select-apsrtc']")));
        droppingPoint.selectByIndex(1);

Let me know if you have any question about this.

Upvotes: 1

dcsohl
dcsohl

Reputation: 7406

Use findElements() instead of findElement(). This will return you a List<WebElement> instead of a single WebElement, and you can choose the first or second item in the list.

Upvotes: 1

Related Questions