首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用selenium webdriver从列表框中逐个选择所有选项

使用selenium webdriver从列表框中逐个选择所有选项
EN

Stack Overflow用户
提问于 2014-07-03 16:51:17
回答 2查看 6.6K关注 0票数 0
代码语言:javascript
运行
复制
 ***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>

我的代码

代码语言:javascript
运行
复制
    Select selectBox = new Select(driver.findElement(By.id("type")));
    List<WebElement> selectOptions = selectBox.getOptions();
    for (WebElement temp : selectOptions) 
    {


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

*输出显示7次。

汽车屋,船,橡树地,汽车屋,船,梧桐树地,汽车屋,船,树地,所有汽车屋,船,树地

代码语言:javascript
运行
复制
I would like to itterate through each options 1 times  and select them.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-04 07:36:46

代码语言:javascript
运行
复制
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();
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-03 21:26:49

我不太熟悉Java中的WebDriver,但是在C#中您可以这样做:

代码语言:javascript
运行
复制
foreach (var elem in selectOptions){
   elem.Click(); //or elem.SendKeys(Keys.Enter);
}

请参阅已经回答的a duplicate question

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24559367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档