无法从下拉列表中选择值.尝试了所有的可能性,但没有运气



这是我的代码试验,

package com.qa.pages;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.qa.base.TestBase;
import com.qa.commonutils.CommonUtils;

public class Loginpage extends TestBase {
    //Page factory(OR)
    //tooltip of abhibus image xpath
    @FindBy(xpath="//div//a//img[contains(@title,"abhibus.com - India's Fastest Online bus ticket booking site")]")
    static WebElement titletooltip;
    //id for leaving from field
    @FindBy(id="source")
    static WebElement leavingfrom;
    //id for going to  field
    @FindBy(id="destination")
    static WebElement goingto;
    //id for date picker for date of journey
    @FindBy(id="datepicker1")
    static WebElement dateofjourney;
    //id for date picker for date of return
    @FindBy(id="datepicker2")
    static WebElement dateofreturn;
    //Click on search button
    @FindBy(xpath="//div[@id='roundTrip']//a[contains(@title,'Search Buses')]")
    static WebElement searchbutton;
    @FindAll( {@FindBy(xpath = "//li[@class='ui-menu-item']//parent::li")} )
    static List<WebElement>listofdropdownleaving;
    @FindBy(xpath = "//li[@class='ui-menu-item']//parent::li")
    static WebElement leavingdropdownpath;
    @FindAll( {@FindBy(xpath = "//li[@class='ui-menu-item']//parent::ul//following::ul//li")} )
    static List<WebElement>listofdropdowngoingto;
    @FindBy(xpath = "//li[@class='ui-menu-item']//parent::ul//following::ul//li")
    static WebElement leavingdropdowngoingto;
    @FindAll( {@FindBy(xpath = "//div[@class='col2']//span//parent::a")} )
    static List<WebElement> selectseatbtn;
    @FindAll( {@FindBy(xpath = "//div[@id='seatSelect1']//li[@class='sleeper available']//parent::a")} )
    static List<WebElement> selectionofseats;
    @FindBy(xpath="//a[@id='O7-2ZZ']")
    static WebElement selectseat;
    @FindBy(xpath="//span[@id='totalfare']")
    static WebElement totalamount;
    @FindBy(xpath="//select[@id='boardingpoint_id1']")
    static WebElement boardingpoint;
    @FindAll( {@FindBy(xpath = "//div[@class='clearfix']//select[@id='boardingpoint_id1']")} )
    static List<WebElement>listofdropdownvalue;
    @FindBy(xpath="//div[@class='red-landmark']")
    static WebElement boardingpointaddress;


public Loginpage() {
    PageFactory.initElements(driver, this);
}

public static void verifysearchbusfun() {
    leavingfrom.sendKeys(Prop.getProperty("leavingfrom"));
    tempExplicityWait(listofdropdownleaving,"Hyderabad Airport",leavingdropdownpath);
    goingto.sendKeys(Prop.getProperty("goingto"));
    tempExplicityWait(listofdropdowngoingto,"Bangalore",leavingdropdowngoingto);
    String dateval=Prop.getProperty("dateval");
    selectDateByJs(driver,dateofjourney,dateval);
    String dateofreturnfromconfig=Prop.getProperty("dateofreturn");
    selectDateByJs(driver,dateofreturn,dateofreturnfromconfig);
    searchbutton.click();
}
/

/得到了值并尝试了以下方式也没有运气。

public static void tempdrop() {
    List<WebElement>dd = driver.findElements(By.tagName("select"));
    System.out.println("Size of the dropdownvalue" +dd.size());
    for(int i=0;i<dd.size();i++)
    {
        String value =dd.get(i).getText();
        System.out.println("values are from dropdownlist " +value);
        String[] words=value.split("\s");//splits the string based on whitespace    
        for(String w:words){  
        System.out.println("Splitted words value  "+w);  
        if(w.equalsIgnoreCase("Shamshabad-22:10"))
        {
        Select ddvalue= new Select(driver.findElement(By.id("//div//select[@id='boardingpoint_id1']//parent::select//preceding::select")));
        ddvalue.selectByVisibleText(w);
        break;
        }
        }  
    }
    }
    public static void drop() {
        ExplicityWaitmethod(driver.findElement(By.id("//div//select[@id='boardingpoint_id1']//parent::select//preceding::select")));
        Select ddvalue= new Select(driver.findElement(By.id("//div//select[@id='boardingpoint_id1']//parent::select//preceding::select")));
        //ddvalue.selectByIndex(1);
        ddvalue.selectByVisibleText("Shamshabad-22:10");
    //listofdropdownvalue.size();
    //System.out.println("Size of dropdownvalue :" +listofdropdownvalue.size());
}
public static String gettooltip()
{
    actions(titletooltip);
    return titletooltip.getAttribute("title");
}
public static void actions(WebElement pathelement) {
        Actions act = new Actions(driver);
        act.moveToElement(pathelement).build().perform();
    }
public static void selectDateByJs(WebDriver driver,WebElement element,String dateval)
{
    JavascriptExecutor js = ((JavascriptExecutor)driver);
    js.executeScript("arguments[0].setAttribute('value','"+dateval+"');",element);
}
public static void ExplicityWaitmethod(WebElement element) {
    try {
    WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions
            .visibilityOf((element)));
        }
catch (NoSuchElementException e) {
    System.out.println(e.getStackTrace());
} catch (Exception e) {
    System.out.println(e.getStackTrace());
        }
}

public static void selectseatbtn(List<WebElement> list) {
    //**************Clicked on select seat button
            try {
            list.size();
            System.out.println("Size of the seats available in the page " +list.size());
            for(int i=0;i<list.size();i++)
            {
                boolean value =list.get(i).isEnabled();
                if(value==true)
                {
                    list.get(i).click();
                    break;
                }
            }
            }
            catch(Exception e)
            {
              System.out.println("Seats are not available");
            }
}
public static void selectionofseats(List<WebElement> list)
{
    //***********Seat selection
            try
            {
                list.size();
            System.out.println("Available seats in the page  " +list.size());
            for(int j=0;j<list.size();j++)
            {
                boolean seatavailabilty =list.get(j).isEnabled();
                if(seatavailabilty==true)
                {
                    list.get(j).click();
                    break;
                }   
            }
            }
            catch(Exception e)
            {
                e.getMessage();
            }       
}
public static void selectseats() {
    selectseatbtn(selectseatbtn);
    selectionofseats(selectionofseats);
    javascriptexecutor();
    drop();
    //tempdrop();
    //dropdownvalue();
    //gettextfromfield(totalamount);
    //gettextfromfield(boardingpointaddress);
    //selectdropdown();
    /*Select boardingvalue = new Select(driver.findElement(By.id("boardingpoint_id1")));
    boardingvalue.selectByVisibleText("Shamshabad-21:15");
*/

}
public static String gettextfromfield(WebElement element) {
    System.out.println("Value got from the field  "+totalamount.getText());
    return totalamount.getText();
}
public static void javascriptexecutor() {
    JavascriptExecutor js = (JavascriptExecutor)(driver);
    js.executeScript("window.scrollBy(0,250)", "");
}

尝试使用 javascript 执行器并向下滚动并选择的值,但没有运气

public static void dropdownvalue() {
        try
        {
            javascriptexecutor();
            //WebDriverWait wait = new WebDriverWait(driver, 30);
                    //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("boardingpoint_id1"))));
            Select ddvalue= new Select(driver.findElement(By.xpath("//div//select[@id='boardingpoint_id1']//option[text()='Shamshabad-21:15']")));
            boolean value=driver.findElement(By.xpath("//div//select[@id='boardingpoint_id1']//option[text()='Shamshabad-21:15']")).isSelected();
            if(value==true)
            {
                WebElement optionvalue =ddvalue.getFirstSelectedOption();
                System.out.println(optionvalue.getAttribute("value"));
                System.out.println(optionvalue.getText());
                ddvalue.selectByIndex(1);
                ddvalue.selectByVisibleText("Shamshabad-21:15");
            }
        }
        catch(Exception e)
        {
            System.out.println("dropdownvalue is not selected");
            e.getMessage();
        }
        }
    public static void tempExplicityWait(List<WebElement> list,String Name,WebElement xpath) {
        try {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions
                .visibilityOf(xpath));
        list.size();
        System.out.println("Auto Suggest List ::" + list.size());
        for (int i = 0; i < list.size(); i++) {
            System.out.println("Value got from the loop   " +list.get(i).getText());
            if (list.get(i).getText().equalsIgnoreCase(Name)) {
                System.out.println("Selected Value from list " +list.get(i).getText());
                list.get(i).click();
                break;
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println(e.getStackTrace());
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    }
    }

我已经通过为下拉列表创建方法尝试了所有可能性。同样的事情是使用示例代码(硬编码值)。请指导我哪里做错了?谢谢。

Got the solution.Thanks:)
public static void tempdrop() {
    Select oSelect = new Select(driver.findElement(By.tagName("select")));
    List <WebElement> elementCount = oSelect.getOptions();
    System.out.println(elementCount.size());
    for(int i =0; i<elementCount.size() ; i++){
        String sValue = elementCount.get(i).getText();
        System.out.println(sValue);
        }
    oSelect.selectByVisibleText("Shamshabad-21:15");
    }

最新更新