如何从Selenium webdriver java中的列表中检查一个项目

时间:2014-08-19 10:57:23

标签: selenium-webdriver

public class Test1 {
 public static void main(String[] args) {

      WebDriver driver = new FirefoxDriver();
      driver.get("http://shop.wikimedia.org");
      WebElement dropDownSelectWebElement = driver.findElement(By.xpath("//*[@id='currencies']"));
      Select dropDownSelect = new Select(dropDownSelectWebElement);
      String currency = "Euro (EUR)";
      dropDownSelect.selectByVisibleText(currency);
      List <WebElement>currencyList = new ArrayList<WebElement>();
      currencyList = dropDownSelect.getOptions();
      Iterator currencyIterator = currencyList.iterator();
      WebElement actualCurrencyName = null;
      while(currencyIterator.hasNext()){

       //System.out.println(currencyIterator.next());
       actualCurrencyName = (WebElement) currencyIterator.next();
       //System.out.println(actualCurrencyName.getText());


      }
      //System.out.println(currencyList);

      if(currencyList.contains("Euro (EUR)"))//I am not able to find out the Euro available in the list or not
      {
       System.out.println("This currency is exist in the List");
      }
      else 
      {
       System.out.println("Get out...");
      }
     }

}

请帮助我,检查是否有选项列表。

2 个答案:

答案 0 :(得分:0)

您需要循环元素列表

for(WebElement a:currencyList){

String str=a.getText();

if(str.contains("Euro (EUR)"))//You should able to find Euro available in the list or not
      {
       System.out.println("This currency is exist in the List");
      }
      else 
      {
       System.out.println("Get out...");
      }
}

答案 1 :(得分:0)

请找到以下代码可以帮助您:

WebDriver driver = new FirefoxDriver();
String strCurrency="Euro (EUR)";
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
driver.get("http://shop.wikimedia.org/");

WebElement lbCurrencies=driver.findElement(By.id("currencies"));

Select select=new Select(lbCurrencies);
List<WebElement> elements=select.getOptions();
boolean found=false;
for(WebElement ele:elements)
{
    if(strCurrency.equals(ele.getText()))
    {
        found=true;
        break;
    }
}
if(found)
    System.out.println("Currency Value "+strCurrency+" exists");
else
    System.out.println("Currency Value "+strCurrency+" does notexists");

driver.close();