【问题标题】:Select All options one by one from a listbox using selenium webdriver使用 selenium webdriver 从列表框中一一选择所有选项
【发布时间】:2014-08-24 21:53:00
【问题描述】:
 ***HTML***
    <select id="type" class="dropdownValues" name="type">
    <option class="dropdownValues" selected="selected" value="00">All</option>
    <option class="dropdownValues" value="01">Car</option>
    <option class="dropdownValues" value="02">House</option>
    <option class="dropdownValues" value="03">Boat</option>
    <option class="dropdownValues" value="04">Plane</option>
    <option class="dropdownValues" value="05">Tree</option>
    <option class="dropdownValues" value="06">Land</option>
    </select>

我的代码

    Select selectBox = new Select(driver.findElement(By.id("type")));
    List<WebElement> selectOptions = selectBox.getOptions();
    for (WebElement temp : selectOptions) 
    {


     System.out.println(temp.getText());
        }

***输出显示 7 次。
全部
车 屋 船 飞机 树 土地 全部 车 屋 船 飞机 树 土地 全部 车 屋 船 飞机 树 土地 全部 车 屋 船 飞机 树 土地 全部 车 屋 船 飞机 树 土地 全部 车 屋 船 飞机 树 土地

I would like to itterate through each options 1 times  and select them.

【问题讨论】:

标签: selenium-webdriver


【解决方案1】:
public static void main(String[] args)
{
    WebDriver driver=new FirefoxDriver();
    driver.get("file:///D:/Programming%20Samples/SelectOptions.html");

    WebElement item=new WebDriverWait(driver,60)
                    .until(ExpectedConditions.elementToBeClickable(By.id("type")));
    Select listItem=new Select(item);
    for(Integer i=0;i<listItem.getOptions().size();i++)
    {
        listItem.selectByIndex(i);
        System.out.println(listItem.getFirstSelectedOption().getText()); //Just to make sure what is selected
    }
    driver.close();
}

【讨论】:

    【解决方案2】:

    我对 Java 中的 WebDriver 不是很熟悉,但在 C# 中你会这样做:

    foreach (var elem in selectOptions){
       elem.Click(); //or elem.SendKeys(Keys.Enter);
    }
    

    请参阅已回答的a duplicate question

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 2011-07-13
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多