简体   繁体   中英

Unable to select the value in the drop down in selenium Web Driver for the website Goibibo

This is the code which I am using. I want to select different values from the "class" drop down. I am getting the count of the no. of drop downs as correct but the values are not getting selected under the dropdown.

    package selectclasspackage;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    import org.junit.Test;
    //import org.junit.Before;
    import static javax.swing.JOptionPane.showMessageDialog;

    import java.util.List;

    import org.openqa.selenium.WebElement;

    public class selectclass {

        @Test
        public void Sample () throws InterruptedException {
             System.setProperty("webdriver.gecko.driver", "C:\\\\Users\\Rajiv\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
             WebDriver driver = new FirefoxDriver ();

             String URL = "https://www.goibibo.com/";
             driver.get(URL);
            driver.manage().window().maximize();
            Thread.sleep(3000);


            Select seatingclass = new Select (driver.findElement(By.id("gi_class")));
            List <WebElement> elementCount = seatingclass.getOptions();
            System.out.println(elementCount.size());

            seatingclass.selectByVisibleText ("Premium Economy");
            System.out.println("Premium Economy selected");
            Thread.sleep(3000);


            for (int i = 0; i < elementCount.size(); i++)
            {
               new Select(driver.findElement(By.id("gi_class"))).selectByIndex(i);
            }


            seatingclass.selectByIndex(2);
            System.out.println("First Class selected");
            Thread.sleep(3000);

            seatingclass.selectByValue("B");
            System.out.println("Business Class selected");
            Thread.sleep(3000);

                }
        }

Why are you using for loop? selectByValue works fine as the number of options in #selnoOfAdults are fixed. The below code worked fine. It selected 10 in the #selnoOfAdults .

Select seatingclass = new Select(driver.findElement(By.id("selnoOfAdults")));
seatingclass.selectByValue("10");

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