使用selenium webdriver从列表框中选择值

时间:2013-12-26 09:02:29

标签: java selenium selenium-webdriver

我需要使用selenium webdriver从列表框中选择1,2,3,4,5,...之类的日期如何选择这个请帮助我...

html代码如下:

select id="dates" multiple="multiple" name="dates[]">
<option label="01" value="1">01</option>
<option label="07" value="7">07</option>
<option label="08" value="8">08</option>
<option label="09" value="9">09</option>
<option label="10" value="10">10</option>
<option label="11" value="11">11</option>
<option label="12" value="12">12</option>
<option label="13" value="13">13</option>
<option label="14" value="14">14</option>
<option label="15" value="15">15</option>
<option label="16" value="16">16</option>
<option label="17" value="17">17</option>
<option label="18" value="18">18</option>
<option label="19" value="19">19</option>
<option label="20" value="20">20</option>
<option label="21" value="21">21</option>
<option label="22" value="22">22</option>
<option label="23" value="23">23</option>
<option label="24" value="24">24</option>
<option label="25" value="25">25</option>
<option label="26" value="26">26</option>
<option label="27" value="27">27</option>
<option label="28" value="28">28</option>
<option label="29" value="29">29</option>
<option label="30" value="30">30</option>
<option label="31" value="31">31</option>
</select>

2 个答案:

答案 0 :(得分:0)

WebElement elem = driver.findElement(By.id("dates"));
new Select(elem).selectByVisibleText("01");

答案 1 :(得分:0)

public class MultiSelectDropDown
{
    public WebDriver driver;

    @Before
    public void setUp()
    {
        driver=new FirefoxDriver();
        driver.get("file:///C:/Users/vkiran/Desktop/dropdown.html");
    }

    @Test
    public void multiSelectTest() 
    {
        Select dateDropDown=new Select(driver.findElement(By.xpath("//*[@id='dates']")));
        dateDropDown.selectByVisibleText("01");
        dateDropDown.selectByVisibleText("07");
        dateDropDown.selectByVisibleText("18");
        dateDropDown.selectByVisibleText("19");
        dateDropDown.selectByVisibleText("20");
    }
}