webdriver --API--(java版) the second part

來源:互聯網
上載者:User

標籤:nbsp   equal   selected   option   序列   UI   oar   false   rate   

操作下拉式清單

<html><body>            <select name=‘fruit‘ size=6>                   <option id=‘peach‘  value=‘taozi‘>桃子</option>                   <option id=‘maybush‘  value=‘shanzha‘>山楂</option>                   <option id=‘litchi‘  value=‘lizhi‘>荔枝</option>                   <option id=‘orange‘  value=‘juzi‘>橘子</option>            </select></body></html>

 

  @Test  public void operateDropList() {      Select dropList =new Select(driver.findElement(By.name("fruit")));      //判斷下拉式清單是否可以複選,因為是單選列表所以用False,多選用true;      Assert.assertFalse(dropList.isMultiple());      //getFirstSelectedOption()方法擷取下拉式清單第一個屬性,getText()方法擷取選項文本文字      Assert.assertEquals("桃子", dropList.getFirstSelectedOption().getText());      //通過序號來鎖定下拉選項,從0開始為第一個下拉元素;      dropList.selectByIndex(3);      //通過value值來進行選中操作      dropList.selectByValue("shanzha");      //dropList.selectByIndex(3).getText()方法報錯,占時不知道什麼原因,都引用dropList.getFirstSelectedOption().getText()進行斷言      //Assert.assertEquals("山楂", dropList.selectByIndex(3).getText());      //通過選項文字進行選中,dropList.selectByVisibleText("荔枝").getText()報錯了;     // Assert.assertEquals("荔枝", dropList.selectByVisibleText("荔枝").getText());      List<String>expect_options=Arrays.asList((new String[]{"桃子","山楂","荔枝","橘子"}));      List<String>actual_options=new ArrayList<String>();      //dropList.getOptions()擷取下拉式清單所有選項文本      for(WebElement option : dropList.getOptions()){          actual_options.add(option.getText());      }      Assert.assertEquals(expect_options.toArray(), actual_options.toArray());      //多選下拉式清單方法也是同上,注意斷言多選dropList.isMultiple()使用assertTrue()
//dropList.deselectByIndex(0);取消第一個下拉元素的選項

 

webdriver --API--(java版) the second part

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.