在Selenium Java中选择多下拉列表

时间:2017-07-16 17:55:28

标签: java selenium

任何人都可以帮我解决查询问题

有2个下拉列表。一个下拉列表包含所有“国家/地区”,第二个下拉列表包含所有“国家/地区”。您必须从“Country”Dropdow中获取所有值。选择国家/地区为“美国”,并且必须计算,检索,验证国家(“美国”)的所有“状态”(在第二个下拉列表中)。我已经编写了代码但无法完成

public class test {
static{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");  
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver= new FirefoxDriver();

driver.manage().window().maximize();
int count=0, total=0;


driver.get("file:///C:/Users/usetr/Desktop/Country.html");

WebElement alllistBox = driver.findElement(By.id("Country"));


Select slt = new Select(alllistBox);
//Select Country India
slt.selectByVisibleText("India");

//Printing All country in dropdown and thier counts
List<WebElement> alloptions = slt.getOptions();
for (WebElement option : alloptions) {

System.out.println(option.getText());
count++;
}
System.out.println("Number of country present " + count);

1 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

假设您要从DropDown检索所有选项,计算并验证所选选项,您可以考虑使用以下代码块。此代码块会记录到Facebook,从Day DropDown获取所有选项,然后选择28作为日期,使用预定义值28验证日期,打印出结果PassFail

import java.util.List;

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 Q45131741_DropDown_options 
{

    static
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");  
    }

    public static void main(String[] args) 
    {

        String date_to_select = "28";
        WebDriver driver= new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");
        WebElement birthdayDay = driver.findElement(By.id("day"));
        Select selectDay = new Select(birthdayDay);
        List<WebElement> day_list = selectDay.getOptions();
        System.out.println("The options for this DropDown are : ");
        for (WebElement day:day_list)
        {
            System.out.println(day.getAttribute("innerHTML"));
        }
        selectDay.selectByVisibleText("28");
        String date_selected = selectDay.getFirstSelectedOption().getText();
        System.out.println("Date selected is : "+date_selected);
        System.out.println("Date to be selected was : "+date_to_select);
        if(date_selected.contains(date_to_select))
        {
            System.out.println("Testcase Passed");
        }
        else
        {
            System.out.println("Testcase Failed");
        }
    }
}

如果这回答你的问题,请告诉我。