从具有相同类名的两个下拉列表中的第一个中选择一个选项

时间:2016-02-02 18:52:30

标签: java selenium xpath drop-down-menu

我选择了最低票价并点击了查看座位,并尝试从第一个下拉列表(登机点)中选择一个选项,但由于两者都具有相同的类名,因此无法执行此操作。感谢帮助。

我使用的代码是:

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通过执行上面的代码,我得到以下异常:

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我已经修改了你提到的代码的最后一部分,但是我得到了与上面相同的异常。

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,我按照你的建议修改了代码如下:

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();

但是,我得到以下例外:

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

2 个答案:

答案 0 :(得分:1)

使用findElements()代替findElement()。这将返回List<WebElement>而不是单WebElement,您可以选择列表中的第一个或第二个项目。

答案 1 :(得分:1)

您可以使用以下定位器(css​​选择器)来唯一标识两个下拉列表。 登机点下拉的定位器将是:

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

Dropping point下拉的定位器将是:

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

如果需要,您也可以使用xPath。您已经有一个正确的xPath用于登机ponit。 对于丢弃点使用:

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

然后你可以做类似的事情:

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);

如果您对此有任何疑问,请与我们联系。