Selenium脚本,用于根据用户从控制台给出的值从下拉列表中选择值

时间:2014-10-09 03:30:23

标签: selenium selenium-webdriver webdriver

我添加了一个图片(Stripe.com)我想根据用户从控制台传递的内容从下拉列表中选择一个特定值。请帮我解决这个问题。

根据我的代码,它从下拉列表中取零值以与用户值进行比较。

这是

 dropdown=driver.findElements(By.xpath("//ul[@id='select2-results-18']//li[@role='presentation']/div/span[@class='card-name']"));
       int a1=dropdown.size();
       System.out.println(a1);
   for(int i=0;i<a1;i++)
   {
       System.out.println("hhh");
      String s1=dropdown.get(i).getText();
      System.out.println(s1);
         if((s1.compareToIgnoreCase(card))==0)
         {
             dropdown.get(i).click();   
           break;

           }
         else
             System.out.println("Fail");
       }

3 个答案:

答案 0 :(得分:0)

这里使用Dropdown元素类型,它具有内置方法,可以通过text / value或index选择下拉选项。

SelectElement dropdown = driver.findElement(By.xpath("//ul[@id='select2-results-18']//li[@role='presentation']/div/span[@class='card-name']"));
dropdown.SelectByText(userInput);

答案 1 :(得分:0)

Select dropdown = new Select(driver.findElement(By.xpath("XXXXX"));

List<WebElement> list = dropdown.getOptions();
for (WebElement temp : list) { 
    if (temp.getText().equals("your value").toString()) {
    System.out.println("Pass");
    }
    else{
        System.out.println("Try again");

}

答案 2 :(得分:0)

试试这个:

Select sel=new Select(wd.findElement(By.xpath("//ul[@id='select2-results-18']//li[@role='presentation']/div/span[@class='card-name']")));
    sel.selectByVisibleText(card);

导入导入org.openqa.selenium.support.ui.Select;使用选择。

请确保选择选择元素xpath。